0

我正在尝试从 EditText 中拉出一条线并与图钉进行比较。如果是正确的密码,则显示授权,否则显示错误密码。我不能让 1234 等于 1234 - 它总是说 Pin Invalid。

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();
}
4

2 回答 2

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();
    }   
于 2012-08-13T18:48:44.183 回答
0

尝试使用

pint.equals("1234")

代替

品脱 == “1234”

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

于 2012-08-13T18:46:05.620 回答