简体   繁体   中英

Android: Get text Editext == item array

I have this code

if(edt_i.getText().toString() == Arrays.asList(coupon).contains("VIP537129")) {
                Toast.makeText(MainActivity.this,"50% ", 
                        Toast.LENGTH_SHORT).show();

My Array

String[] coupon = {
        "MEM537128",
        "MEM537129",
        "VIP537128",
        "VIP537129"
    };

I want to check text in Edittext with Array, But didn't working. Please help me!

EDIT

        if (Arrays.asList(coupon).contains(edt_i.getText().toString())) {
                Toast.makeText(MainActivity.this,"10% ", 
                        Toast.LENGTH_SHORT).show();
            }else if(Arrays.asList(coupon).contains(edt_i.getText().toString())) {
                Toast.makeText(MainActivity.this,"20% ", 
                        Toast.LENGTH_SHORT).show();
            }else if(Arrays.asList(coupon).contains(edt_i.getText().toString())) {
                Toast.makeText(MainActivity.this," 30% ", 
                        Toast.LENGTH_SHORT).show();
            }else if(Arrays.asList(coupon).contains(edt_i.getText().toString())) {
                Toast.makeText(MainActivity.this,"50% ", 
                        Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this," False ", 
                        Toast.LENGTH_SHORT).show();
            }

But Toast only show 10%

not sure about your question. is this what u need ?

int[] percent = {10,20,30,50};


 if(Arrays.asList(coupon).contains(edt_i.getText().toString())) 
{
Toast.makeText(MainActivity.this,percent[Arrays.asList(bigger).indexOf(edt_i.getText().toString())].toString()+"%", 
                                        Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this," False ", 
                            Toast.LENGTH_SHORT).show();
}

You need to check a String contains in String Array, like this,

if(Arrays.asList(coupon).contains(edt_i.getText().toString())) {
     Toast.makeText(MainActivity.this,"50% ",Toast.LENGTH_SHORT).show();
}

you must to use method indexOf in order to find position of inputText in coupon array.

    String inputText = edt_i.getText().toString();
    switch (Arrays.asList(test).indexOf(inputText)) {
        case 0:
            Toast.makeText(MainActivity.this,"10% ", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            Toast.makeText(MainActivity.this,"20% ", Toast.LENGTH_SHORT).show();
            break;
        case 2:
            Toast.makeText(MainActivity.this,"30% ", Toast.LENGTH_SHORT).show();
            break;
        case 3:
            Toast.makeText(MainActivity.this,"50% ", Toast.LENGTH_SHORT).show();
            break;
        default:
            Toast.makeText(MainActivity.this," False ", Toast.LENGTH_SHORT).show();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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