如何在Android Studio中 "分组 "if语句在spinner中使用?

问题描述 投票:0回答:1

我是JavaAndroid Studio的新手,我在 "分组 "方面遇到了麻烦,如何在spinner中使用if语句。基本上,我需要我的应用程序根据微调器设置的位置来使用某些值。这是我目前的代码。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //Instantiating Widgets

        firstInput = findViewById(R.id.firstInput);
        secondInput = findViewById(R.id.secondInput);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        spinner = findViewById(R.id.spinner);


        //Spinner - adapter etc.
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        //Adding a click event for button (Executing the convert method when clicked)
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




           int force = 0;
           double in1 = Double.valueOf(firstInput.getText().toString());
           double in2 = Double.valueOf(secondInput.getText().toString());




           //Grade 5 Fine Thread
                if (in1 / in2 == 0.25) {
                        force = 2325;
                } else if (in1 / in2 == 0.3125) {
                        force = 3675;
                } else if (in1 / in2 == 0.375) {
                        force = 5588;
                } else if (in1 / in2 == 0.4375) {
                        force = 7575;
                } else if (in1 / in2 == 0.5) {
                        force = 10200;
                } else if (in1 / in2 == 0.5625) {
                        force = 12975;
                } else if (in1 / in2 == 0.625) {
                        force = 16350;
                } else if (in1 / in2 == 0.75) {
                        force = 23775;
                } else if (in1 / in2 == 0.875) {
                        force = 32475;
                }




             //Grade 5 Coarse Thread
             if(in1 / in2 == 0.25) {
                 force = 2025;
             } else if(in1 / in2 == 0.3125) {
                 force = 3338;
             } else if(in1 / in2 == 0.375){
                 force = 4950;
             } else if(in1 / in2 == 0.4375){
                 force = 6788;
             } else if(in1 / in2 == 0.5){
                 force = 9075;
             } else if(in1 / in2 == 0.5625){
                 force = 11625;
             } else if(in1 / in2 == 0.625){
                 force = 14400;
             } else if(in1 / in2 == 0.75){
                 force = 21300;
             } else if(in1 / in2 == 0.875){
                 force = 29475;
             }






        textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));


            }
        });
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();

        if (position == 0) {

        }
        if (position == 1) {

        }



    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

因此,如果位置设置为0,我需要将力设置为细线程值,如果位置设置为1,则需要将力设置为粗线程值。我不知道该怎么做。任何帮助都非常感激。

谢谢你的帮助。

java android class methods statements
1个回答
1
投票

如果我误解了问题,请告诉我,但我认为这将为你工作。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        double in1 = Double.valueOf(firstInput.getText().toString());
        double in2 = Double.valueOf(secondInput.getText().toString());
        if (spinner.getSelectedItemPosition() == 0) {
            //fine thread code
            ...
        } else {
            //coarse thread code
            ...
        }
        textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.