简体   繁体   English

如果 Spinner 下拉列表中的条件

[英]If condition on Spinner drop-down list

I made a drop-down list using spinner but now I want and a specific message to display when a certain value is selected along with an EditText when a certain keyword is entered.我使用微调器创建了一个下拉列表,但现在我希望在选择某个值时显示一条特定消息,并在输入某个关键字时显示一个 EditText。 I attempted using if condition but it didn't work.我尝试使用 if 条件,但它不起作用。 The scenario is like this: When the user input their age over 18 and selects "Yes" from the drop-down hire , a certain message will appear, both conditions must be satisfied场景是这样的:当用户输入自己的age超过 18 岁并从下拉hire中选择“是”时,会出现一条消息,必须同时满足两个条件

 public void stan(View hiring){
        ArrayAdapter<CharSequence> hire=ArrayAdapter.createFromResource(this,R.array.decide, android.R.layout.simple_spinner_item);
        hire.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(hire);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String decide =spinner.getSelectedItem().toString()
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        int age= Integer.parseInt(aged.getText().toString());
        if(age>18 && spinner.equals("Yes")){
            Toast.makeText(this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"no",Toast.LENGTH_LONG).show();
        }

Try this尝试这个

public void stan(View hiring){
        ArrayAdapter<CharSequence> hire=ArrayAdapter.createFromResource(this,R.array.decide, android.R.layout.simple_spinner_item);
        hire.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(hire);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String decide =spinner.getSelectedItem().toString();
                int age= Integer.parseInt(aged.getText().toString());
                if(age>18 && decide.equals("Yes")){
                Toast.makeText(this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
                }else{
                Toast.makeText(this,"no",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

Use spinner.getSelectedItem().toString()使用spinner.getSelectedItem().toString()

Finally, Your condition will be最后,您的情况将是

if(age>18 && spinner.getSelectedItem().toString().equals("Yes")){
             Toast.makeText(YourActivityName.this,"Ok, go eat cabbage sauce",Toast.LENGTH_LONG).show();
            }else{
             Toast.makeText(YourActivityName.this,"no",Toast.LENGTH_LONG).show();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM