选择一个微调器项目来更改文本视图中的文本?

时间:2016-02-25 20:50:02

标签: java android textview android-spinner

我需要为Android的这个转换应用程序提供一些帮助。 所以我有一个Spinner填充了这些项目:

 <string name="action_settings">Settings</string><string-array name="ConversionItems">
         <item >Miles to Kilometers</item>
         <item >Kilometers to Miles</item>
         <item >Inches to Centimeters</item>
         <item >Centimeters to Inches</item>
 </string-array>

这是我的MainActivity中的一些代码

> conversionspinner.setOnItemSelectedListener(new
> OnItemSelectedListener() {
>       @Override
>       public void onItemSelected(AdapterView<?> parent, View view, int position,
>               long id) {
>           // TODO Auto-generated method stub
>           
>       }
> 
>       @Override
>       public void onNothingSelected(AdapterView<?> parent) {
>           // TODO Auto-generated method stub
>           return;
>       }
>     }

我想知道的是,当选择特定项目时,如何更改文本视图的文本。例如,如果选择“Miles to Kilometers”,textview1将显示“Miles”,textview2将显示“公里”。我是Android编程和编程的新手,所以简单的解决方案会很棒!

由于

2 个答案:

答案 0 :(得分:1)

非常简单:

    final String CONDITION_1 = "Kilometers";
    final String CONDITION_2 = "Miles";

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    int units = 10;

    private void updateText() {
         String spinnerState = conversionspinner.getSelectedItem().toString();


         if (spinnerState.equalsIgnoreCase("")) {
             textView1.setText("");

         } else if (spinnerState.equalsIgnoreCase(CONDITION_1)){
             //do appropriate unit conversion:
             units = units * (8/5);
             textView1.setText(String.valueOf(units) + spinnerState);

         } else if (spinnerState.equalsIgnoreCase(CONDITION_2)) { 
             units = units * (5/8);
             textView1.setText(String.valueOf(units) + spinnerState);
         }
    }

conversionspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          updateText();

   });

答案 1 :(得分:1)

你可以做这样的事情

conversionspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
           //position will tell you which position is selected
           // in your array  index 0 - <item >Miles to Kilometers</item>
           //                index 1 - <item >Kilometers to Miles</item>
           //                index 2 - <item >Inches to Centimeters</item>
           //                index 3 - <item >Centimeters to Inches</item>

            switch(position){
                case 0:
                    textView1.settext("Miles");
                    textView2.settext("Kilometers");
                    break;
                case 1:
                    textView1.settext("Kilometer");
                    textView2.settext("Miles");
                    break;
                case 2:
                    textView1.settext("Inches");
                    textView2.settext("Centimeter");
                    break;
                case 3:
                    textView1.settext("Centimeter");
                    textView2.settext("Inches");
                    break;
            }


        }

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

        }

        });

希望有所帮助!