简体   繁体   English

如何在 Android Studio 的微调器中“分组” if 语句?

[英]How do I “group” if statements to use in a spinner in Android Studio?

I'm quite new to Java/Android Studio and I'm having troubles "grouping"?我对 Java/Android Studio 还很陌生,并且在“分组”时遇到了麻烦? if statements to use in a spinner.在微调器中使用的 if 语句。 Basically I need my app to use certain values based on which position the spinner is set to.基本上我需要我的应用程序根据微调器设置的 position 使用某些值。 This is my current code.这是我当前的代码。

 @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) {

    }
}

So if the position is set to 0, I need the force to be set to fine thread values and if position is set to 1, the force needs to be set to the coarse thread values.因此,如果 position 设置为 0,我需要将力设置为细螺纹值,如果 position 设置为 1,则需要将力设置为粗螺纹值。 I'm not sure where to go with this.我不确定 go 在哪里。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Thank you.谢谢你。

Please let me know if I misunderstood the question, but I think this will work for you:如果我误解了这个问题,请告诉我,但我认为这对你有用:

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM