1

我在我的应用程序中使用 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");
    }
}
4

2 回答 2

1

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

假设你打电话给你的 spinner 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
}

祝你好运。

于 2012-12-13T08:44:47.160 回答
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");
    }
}
于 2012-12-13T13:59:16.363 回答