当两个微调器等于特定字符串时,如何允许调用函数?

时间:2018-07-17 10:41:20

标签: java android android-studio mobile

我制作了2个微调器,它们都由一系列不同类型的重量组成。这些是“磅”,“石头”,“克”,“千克”。我需要能够检查用户从这两个微调器中选择的选项,然后根据他们选择的内容来调用函数。因此,例如,如果第一个微调器等于“ Lbs”,第二个微调器等于“ Grams”,则将调用一个函数。

这些是我的琴弦

  <string-array name="weightFSpinner">
    <item>Lbs</item>
    <item>Grams</item>
    <item>Kilograms</item>
    <item>Stone</item>
    <item>Ounces</item>
</string-array>

<string-array name="weightSSpinner">
    <item>Lbs</item>
    <item>Grams</item>
    <item>Kilograms</item>
    <item>Stone</item>
    <item>Ounces</item>
</string-array>

这是我的体重页面上的代码

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight_page);
        // Code for first drop down list
        final Spinner weightFSpinner = (Spinner) findViewById(R.id.weightFSpinner);
        final ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(weightPage.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.weightFSpinner));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        weightFSpinner.setAdapter(myAdapter);


        // code for second drop down list
        final Spinner weightSSpinner = (Spinner) findViewById(R.id.weightSSpinner);
        final ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(weightPage.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.weightSSpinner));
        myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        weightSSpinner.setAdapter(myAdapter2);



        Button convertBtn = (Button) findViewById(R.id.convertBtn);
        convertBtn.setOnClickListener(new View.OnClickListener() {

            //when the button is pressed this happens
            @Override
            public void onClick(View view) {

                if(myAdapter.equals("Lbs") && myAdapter2.equals("Grams")){
                    poundsToGrams();
                }


            }

            //converting pounds to grams
            private void poundsToGrams() {
                EditText convertTxt = (EditText) findViewById(R.id.convertTxt);
                TextView resultTxt = (TextView) findViewById(R.id.resultTxt);
                String firstWeight;
                String secondWeight;
                double pounds;

                try {
                    pounds = parseDouble(convertTxt.getText().toString());

                } catch
                        (NumberFormatException ex) {
                    convertTxt.setError("Must be int");
                    return;
                }
                double result = (pounds * 453.59237);
                resultTxt.setText(result + " Grams");
            }


            // firstWeight = myAdapter.getItem(0);
            //secondWeight = myAdapter2.getItem(1);


        });
    }
}

3 个答案:

答案 0 :(得分:1)

You can do the following ways:

1: You could share the adapter between different Spinners if they adapted the same information. A single OnItemSelectedListener will work for the 2 Spinners. You can call getId() on the AdapterView passed as an argument to know which Spinner raised the event.

 @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

    switch(parentView.getId()){
        case R.id.weightFSpinner:
            fValue= weightFSpinner.getSelectedItem().toString();
            break;
        case R.id.weightSSpinner:
            sValue= weightSSpinner.getSelectedItem().toString();
            break;
        case ...
    }


}

2: Store the selected values in 2 variables item is selected from spinners.

3: then check the values of variables in click listener of button

答案 1 :(得分:0)

将.onItemSelectedListener侦听器添加到微调器,并覆盖其功能onNothingSelected和onItemSelected。

在两个微调器的onItemSelected中,将该位置的值保存到两个微调器均可访问的字符串。例如,将weightSSpinner的权重保存到字符串“ myWeightS”,将weightFSpinner的权重保存到字符串“ myWeightF”。

然后在两个微调器的onItemSelected函数中,检查字符串是否相等。如果是这样,请执行所需的功能。

package com.whatever.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

import com.whatever.config.ConfigurationSettings;

@Controller("/")
public class MainController{

    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        System.out.println("WELCOME!");

        ModelAndView mAndView = new ModelAndView();

        mAndView.setViewName("welcome");
        return mAndView;
    }
}

答案 2 :(得分:0)

将一个onItemSelectedListener添加到微调器

     weightFSpinner.setOnItemSelectedListener(
                    new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(
                                AdapterView<?> parent, View view, int position, long id) {

// here you can add the if clauses to check which spinner contains which value and call the method accordingly 


                        }

                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });