gpt4 book ai didi

java - 警报对话框的 inflated layout 不起作用

转载 作者:行者123 更新时间:2023-12-01 09:38:22 30 4
gpt4 key购买 nike

我有以下方法,在允许用户访问“设置”菜单之前,我必须确认用户的密码。为此,我让他再次登录,因为 Parse(我的云存储)不允许我确认密码。到目前为止,一切都很好。问题是当我将 view 膨胀到我的 AlertDialog 以使用自定义 buttonEditText 时。您可以在下面的代码中看到我对 Button 部分进行了注释,因为当我尝试获取 onClickListener 时它不起作用。它只是什么都不做。所以我目前正在使用 alertDialogPositiveButton 并且没问题。第二个问题也是真正的问题是我无法检索插入到 editText 中的数据。 SUpassword3 始终等于空白,而不是 null。有人可以帮忙吗?

方法:

public void confirmPassword(){

final LayoutInflater inflateSetup = getLayoutInflater();

AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
alertSetup.setView(inflateSetup.inflate(R.layout.inflate_setup, null));
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");
View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);
edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
SUpassword3 = edtPassword3.getText().toString();

final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if(e==null){
//password confirmed, logout last user and go to next setup
System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, logout last user and go to setup");
beforeConfirmPassword.logOut();
}else{
//passwords don't match
System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]");
}
}
});
}
});
final AlertDialog dialogSetup = alertSetup.create();
dialogSetup.show();


/**
btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm);
btnConfirmPassowod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
System.out.println("User has clicked the [confirm] button");
final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if(e==null){
//password confirmed, logout last user and go to next setup
System.out.println("Password confirmed, logout last user and go to setup");
beforeConfirmPassword.logOut();
}else{
//passwords don't match
dialogSetup.dismiss();
Snackbar.make(v, "Wrong password, try again.", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
}
}); */
}

inflate_setup.xml(我在alertDialog上膨胀的xml文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/edtPassword3"
android:layout_gravity="center_horizontal"
android:hint="@string/EN_txt.confirmPassword"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btnConfirm"
android:layout_gravity="center_horizontal" />

</LinearLayout>

最终版本(工作):

public void confirmPassword(){

final LayoutInflater inflateSetup = getLayoutInflater();
final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);

AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
alertSetup.setView(viewInflate_setup);
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");

edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
SUpassword3 = edtPassword3.getText().toString();

...

最佳答案

您在 onClickListener 中膨胀了另一个 View 。这就是为什么您实际上检查另一个 edtPassword3 的文本,而不是用户可见的文本。应该是这样的:

View view = inflateSetup.inflate(R.layout.inflate_setup;
final EditText edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
alertSetup.setView(view, null));
alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("User has clicked the [confirm] button");
SUpassword3 = edtPassword3.getText().toString();
....

关于java - 警报对话框的 inflated layout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38652649/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com