如何使用微调框下拉菜单更改TextView小部件上的文本

时间:2019-10-18 10:32:27

标签: java android xml android-studio

我有实际要做的事。我必须使用Java制作一个测量转换器android应用。它涉及使用微调框下拉菜单。 This link is the image of my practical handout.如图所示,如果用户在微调框下拉菜单中选择英里为公里,则TextView小部件将显示英里和公里。但是我无法使它工作。我现在将为您提供示例代码。

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements 
TextView.OnEditorActionListener, AdapterView.OnItemSelectedListener {
private Spinner conversions;
private String list [];
private TextView u1;
private TextView u2;
private EditText input;
private TextView convertedUnit;

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

    conversions = findViewById(R.id.converterSelector);
    u1 = findViewById(R.id.unit1Lbl);
    u2 = findViewById(R.id.unit2Lbl);
    input = findViewById(R.id.enterNum);
    convertedUnit = findViewById(R.id.convertedNumLbl);
    list = getResources().getStringArray(R.array.conversion_types);



   // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.conversion_types, android.R.layout.simple_spinner_item);
  // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
    conversions.setAdapter(adapter);

    conversions.setOnItemSelectedListener(this);
    u1.setOnEditorActionListener(this);
}


public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
    String menuComponent = parent.getItemAtPosition(pos).toString();

    if(menuComponent.equals(pos)){
        if(pos == 0) {
            u1.setText(getString(R.string.milesLbl));
            u2.setText(getString(R.string.km));


            double mile = (double) 
            Double.parseDouble(String.valueOf(input.getText()));
            double kiloMeter = mile * 1.6093;
            convertedUnit.setText("" + kiloMeter);
        }


    }
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    return false;
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
}

我的应用程序崩溃,并显示以下代码。您能否找出问题所在,如果发现问题,可以粘贴正确的代码来解决吗?谢谢。

1 个答案:

答案 0 :(得分:2)

OnItemSelected,您可以获取该位置并根据该位置执行方法。

 @Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    switch (i){
        case 0: milesToKilo();
        break;
        case 1: kiloTomiles();
        break;
        case 2: inchesToCenti();
        break;
        case 3: centiToInches();
        break;
        default:
    }

}

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

}

public void milesToKilo(){
    u1.setText("Miles");
    u2.setText("Kilos");

    // rest of calculations

}
public void kiloTomiles(){

    u1.setText("Miles");
    u2.setText("Kilos");
    // rest of calculations

}

public void inchesToCenti(){
    u1.setText("inches");
    u2.setText("Centi");
    // rest of calculations

}
public void centiToInches(){
    u1.setText("Centi");
    u2.setText("Inches");
    // rest of calculations

}