如何检查我的编辑文本输入值是否等于数组值

时间:2018-07-16 11:09:19

标签: android arrays android-edittext android-studio-3.0

我要检查我的编辑文本值是否等于数组值 String[] zipcode = {"123","456"}敬酒成功,否则比赛失败。

这是我的代码:

public class MainActivity extends AppCompatActivity {

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

        final EditText et_zipcode = (EditText) findViewById(R.id.etzipcode);
        Button bt_submit = (Button) findViewById(R.id.btsave);

        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] zipcodes = {"123","456"};
                if (et_zipcode.getText().equals(zipcodes)){
                    Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

5 个答案:

答案 0 :(得分:0)

String[] zipcodes = {"123","456"};
   boolean contains = Arrays.asList(zipcodes ).contains(et_zipcode.getText());
    if (contains)
    {Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();}
    else {Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();}

答案 1 :(得分:0)

使用此条件检查编辑文本是否包含邮政编码:

if(Arrays.asList(zip).contains(et_zipcode.getText().toString()))

答案 2 :(得分:0)

尝试此代码可能会对您有所帮助

CardView card = findViewById(R.id.card_view_top);
card.setCardBackgroundColor(Color.parseColor("#E6E6E6"));

答案 3 :(得分:0)

...
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak> Please link your account </speak>"
            },
            "card": {
                "type": "LinkAccount"
            }
...

答案 4 :(得分:0)

 **try this one**

bt_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String[] zipcodes = {"123","456"};
             String entertext = et_zipcode.getText().toString();
                 boolean result=   Arrays.asList(zipcodes).contains(entertext);
                  System.out.println(result);
                    if(result==true ){
                        System.out.println("matched");
                       }else {
                     System.out.println(" not matched"); }

                }
            });
        }