从edittext中提取数据并与charsequence进行比较

时间:2012-08-13 18:44:03

标签: android android-edittext

我正在尝试从EditText中拉出一行并与引脚进行比较。如果它是正确的引脚,则显示授权,否则显示错误的引脚。我不能让1234等于1234 - 它总是说Pin无效。

Context context = this;
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Pin Entry");

final EditText pinEntry = (EditText) dialog.findViewById(R.id.pinAuth);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButton);

dialogButton.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v) 
{
    CharSequence alertText;
    int duration = Toast.LENGTH_SHORT;

    CharSequence pint = pinEntry.getText();

    if(pint != "1234")
    {
        alertText = "Pin invalid.\nPlease try again.";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
        updateList();
    }

    else if(pint == "1234")
    {
        alertText = "Pin authorized";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
    }   
dialog.dismiss();
}
});
dialog.show();
}

2 个答案:

答案 0 :(得分:1)

更改

if(pint != "1234")
    {
        alertText = "Pin invalid.\nPlease try again.";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
        updateList();
    }

    else if(pint == "1234")
    {
        alertText = "Pin authorized";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
    }

if(!(pint.equals("1234")))
    {
        alertText = "Pin invalid.\nPlease try again.";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
        updateList();
    }

    else if(pint.equals("1234"))
    {
        alertText = "Pin authorized";
        Toast toast = Toast.makeText(context, alertText, duration);
        toast.show();
    }   

答案 1 :(得分:0)

尝试使用

  

pint.equals("1234")

而不是

  

pint ==“1234”

您不能对字符串使用!=和==。这些检查引用而不是字符串的实际内容