1
  1. 我需要将微调器与 editText 连接以获得转换器的结果

    微调器和我的转换器之间没有连接。

    public class A01Acre extends Activity {
        Spinner spinnerarea;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.a01acre);  
    
    
            final    EditText editAcre = (EditText) findViewById(R.id.editAcre);        
            final    EditText editHectar = (EditText) findViewById(R.id.editHectar);        
            final    EditText editSquareinch = (EditText) findViewById(R.id.editSquareinch);
            final    EditText editSquarekm = (EditText) findViewById(R.id.editSquarekm);
            final    EditText editSquaremeter = (EditText) findViewById(R.id.editSquaremeter);
            final    EditText editSquaremile = (EditText) findViewById(R.id.editSquaremile);
            final    EditText editSquareyard = (EditText) findViewById(R.id.editSquareyard);
    
            Button buttonConvert = (Button) findViewById (R.id.buttonConvertAcre);
            buttonConvert.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    //try
                    try {
                        //    
                        double acre = Double.valueOf (editAcre.getText().toString());
                        double hectar = acre * 0.404686;
                        double squareinch = acre * 6.273e+6;
                        double squarekm = acre * 0.00404686;
                        double squaremeter = acre * 4046.86;
                        double squaremile = acre * 0.0015625;
                        double squareyard = acre * 4840;
    
    
                        editHectar.setText (String.valueOf(hectar));
                        editSquareinch.setText (String.valueOf(squareinch));
                        editSquarekm.setText (String.valueOf(squarekm));                    
                        editSquaremeter.setText (String.valueOf(squaremeter));
                        editSquaremile.setText (String.valueOf(squaremile));                    
                        editSquareyard.setText (String.valueOf(squareyard));        
    
                        //catch
                    } catch (NumberFormatException ex) {
                        // write a message to users
                        editHectar.setText ("");
                    }
                }
    
    
            });
    
    
            spinnerarea = (Spinner) findViewById(R.id.spinnerarea);
    
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.spinnerTestArray, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerarea.setAdapter(adapter);
            spinnerarea.setOnItemSelectedListener(new MyOnItemSelectdListerne());
        }
    
        @Override
        public void onDestroy(){
            super.onDestroy();
        }
    
    
        public class MyOnItemSelectdListerne implements OnItemSelectedListener {
    
            @Override
            public void onItemSelected(AdapterView<?> patent,
                    View view, int pos, long id) {
                String str = patent.getItemAtPosition(pos).toString().toUpperCase();
            }
    
            @Override
            public void onNothingSelected(AdapterView parent) {
    
    
    
    
            }
        }
    }
    
4

1 回答 1

1

在您的方法中添加一个 switch 语句onItemSelected()- 开启pos

对于每个单位(每个case),您可以重新计算,然后将值输出给用户。

就像是,

switch(pos) {
    case 0:
        //acre - redo calculation
        break;
    case 1:
        //hectare - redo calculation
        break;
    case 2:
        //squareinch - redo calculation
        break;
    //...
    default:
        throw new UnexpectedException();
}

//display results to user
于 2013-01-28T17:40:31.370 回答