Android微调器:执行选中的项目并将其与另一个微调器进行比较

时间:2012-12-13 08:32:12

标签: android android-spinner

我在我的应用程序中使用2个微调器,我想在我的按钮单击中执行此操作,例如在visual studio中这样做

if ((spin1.text = "bla bla bla") && (spin2. text = "ho ho ho"))
    {
       text1.text = result;
    }

你有任何想法在android中执行此操作吗?感谢。

解决了这个问题!!

spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value1 = item.toString();
        }

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

        }
    });

    spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
            Object item = parent.getItemAtPosition(pos);
            value2 = item.toString();
        }

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

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void count(View v)
{
    if(value1.equals("Depok") && value2.equals("Jakarta"))
    {
        hasil.setText("SUCCESS");
    }
}

2 个答案:

答案 0 :(得分:1)

首先你必须正确地从微调器中检索文本,你做到了吗? 如果没有,这是怎么做的:

假设您调用了微调器spinner

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String txtFromSpinner = mySpinner.getSelectedItem().toString();

比较时,从微调器中取出字符串并将其与某个字符串进行比较。

if (txtFromSpinner.equals("bla bla bla") && txtFromSpinner2.equals("ho ho ho")
{
   Log.E("Well done sir.","Comparison complete!");
   //Code
}
祝你好运。

答案 1 :(得分:0)

尝试使用此代码一次。

String item1,item2;
Textview hasil;

hasil = (TextView) findViewById(R.id.textView1);


spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {

    public void onItemSelected(AdapterView<?> parent, View seletedItem, int pos, long id)
       {
          String item1 = (String) parent.getItemAtPosition(pos);
       }

        public void onNothingSelected(AdapterView<?> parent)
        {

        }
});

spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{

        public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
        {
           String item2 = (String) parent.getItemAtPosition(pos);

        }

        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });
}

public void count(View v)
{
    if(item1.equals("Depok") && item2.equals("Jakarta"))
    {
        hasil.setText("berhasil");
    }
}