我有以下方法,我必须在允许访问“设置”菜单之前确认用户的密码。为此,我让他再次登录,因为Parse(我的云存储)不允许我确认密码。到现在为止还挺好。问题是当我将view
扩展到我的AlertDialog
以使用自定义button
和EditText
时。您可以在下面的代码中看到我评论了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();
...
答案 0 :(得分:1)
您在onClickListener
内充气另一个视图。这就是为什么你实际检查另一个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();
....
答案 1 :(得分:0)
尝试使用:Dialog类
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
答案 2 :(得分:0)
获取膨胀视图的参考:
View dialogView = inflateSetup.inflate(R.layout.inflate_setup, null);
在对话框中使用:
alertSetup.setView(dialogView);
然后,使用以下方式访问按钮
Button myButton = (Button) dialogView.findViewById(R.id.btnConfirm);