Android不同的选择微调器显示不同的textview

时间:2014-11-27 11:30:33

标签: android android-studio textview android-spinner

我希望我的微调器在Android Studio中为我的微调器选择不同的选项显示不同的textview。它是一种药物相互作用应用程序,因此当用户从微调器中选择药物时,我希望每种药物都有不同的相互作用。 任何建议都会有用。

以下是我的代码,

    public class MyActivity extends Activity implements OnItemSelectedListener {
    Spinner spinner;
    TextView showmed;
    private String[] state = {"Adalimumab",
            "Etanercept",
            "Tacrolimus",
            "Mycophenolic Acid",
            "Bicalutamide",
            "Darbepoetin alfa",
            "Ciclosporin",
            "Interferon beta-1a",
            "Triptorelin"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        System.out.println(state.length);
        showmed = (TextView) findViewById(R.id.showgender);
        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, state);
        adapter_state
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter_state);
        spinner.setOnItemSelectedListener(this);

     }

    public void onItemSelected(AdapterView<?> parent, View view, int position,
                               long id) {
        spinner.setSelection(position);
        String selState = (String) spinner.getSelectedItem();
        showmed.setText("Your drug:" + selState);
     }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

4 个答案:

答案 0 :(得分:1)

我相信这是您可能正在寻找的方法:

public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {

               switch(position)
{

               case 0:
               showmed.setText("Your drug number 0 on the list");
                break;

               case 1:
               showmed.setText("Your drug number 1 on the list");
                break;

}


 }

您还应该更新为final TextView showmed;

答案 1 :(得分:0)

您可以在微调器上设置侦听器:

spinner.setOnItemSelectedListener(this);

要实现的方法如下所示:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
    if(position == 0)
        textView.setVisibility(View.VISIBLE);
    //do something similar for every position
}

根据传递给该方法的位置,您可以将文字视图的可见性更改为已消失或可见。

答案 2 :(得分:0)

检查药物名称并根据药物名称设置

adapter_state.setDropDownViewResource(R.layout.custom_text_view)

这里的R.layout.custom_text_view是您将根据自己的喜好制作的XML

答案 3 :(得分:-1)

这是我的代码,如果它有帮助。我还能使用这段代码吗?

public class MyActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
TextView showmed;
private String[] state = {"Adalimumab",
        "Etanercept",
        "Tacrolimus",
        "Mycophenolic Acid",
        "Bicalutamide",
        "Darbepoetin alfa",
        "Ciclosporin",
        "Interferon beta-1a",
        "Triptorelin"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    System.out.println(state.length);
    showmed = (TextView) findViewById(R.id.showgender);
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, state);
    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter_state);
    spinner.setOnItemSelectedListener(this);

 }

public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    spinner.setSelection(position);
    String selState = (String) spinner.getSelectedItem();
    showmed.setText("Your drug:" + selState);
 }

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}