Android 自定义对话框EditText

发布于 2025-01-03 11:15:51 字数 1689 浏览 4 评论 0原文

public void createpass() {
//set up dialog
final Dialog dialog = new Dialog(App.this);
dialog.setContentView(R.layout.createpass);
dialog.setTitle("Set Password");
dialog.setCancelable(false);
//there are a lot of settings, for dialog, check them all out!

//set up text
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText("");
//set up text
final EditText text2 = (EditText) dialog.findViewById(R.id.editText2);
text2.setText("");



//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {

    String createpass_password = text.getText().toString().trim();
    String createpass_password2 = text2.getText().toString().trim();


    try
    {
        if(createpass_password == createpass_password2)
        {
            FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/password.db");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(createpass_password);
            //Close the output stream
            out.close();    
        dialog.dismiss();
        }
        else
        {
            toaster("Passwords are not matching !");
            text.setText("");
            text2.setText("");
        }
    }
    catch(Exception x)
    {       
        String ErrorMessage = x.getMessage();
        toaster("Error");
        finish();
    }

}
});
//now that the dialog is set up, it's time to show it   

    dialog.show();

}

我尝试访问 EditText 的值, 他们应该是平等的 但他们不是

我尝试制作密码对话框 并且密码已通过“if else”验证验证,

对话框正确显示,但当我输入相同的值时 if 结构将它们报告为不相等

public void createpass() {
//set up dialog
final Dialog dialog = new Dialog(App.this);
dialog.setContentView(R.layout.createpass);
dialog.setTitle("Set Password");
dialog.setCancelable(false);
//there are a lot of settings, for dialog, check them all out!

//set up text
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText("");
//set up text
final EditText text2 = (EditText) dialog.findViewById(R.id.editText2);
text2.setText("");



//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {

    String createpass_password = text.getText().toString().trim();
    String createpass_password2 = text2.getText().toString().trim();


    try
    {
        if(createpass_password == createpass_password2)
        {
            FileWriter fstream = new FileWriter("/data/data/folder.hide.alexander.fuchs/password.db");
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(createpass_password);
            //Close the output stream
            out.close();    
        dialog.dismiss();
        }
        else
        {
            toaster("Passwords are not matching !");
            text.setText("");
            text2.setText("");
        }
    }
    catch(Exception x)
    {       
        String ErrorMessage = x.getMessage();
        toaster("Error");
        finish();
    }

}
});
//now that the dialog is set up, it's time to show it   

    dialog.show();

}

I try to access the value of EditText,
they should be equal
but they aren't

I try to make password dialog
and the password is verified verify via "if else"

the dialog appears correctly but when i enter the same values
the if structure reports them as not equal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可遇━不可求 2025-01-10 11:15:51

您必须使用 "String".equals("String") 来测试 String 内容。

== 测试对象引用是否相等。

所以在你的代码中你必须这样做:

if (createpass_password.equals(createpass_password2)) {

You have to use "String".equals("String") to test String content.

== tests if the objects references are equal.

So in your code you have to do:

if (createpass_password.equals(createpass_password2)) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文