我正在尝试像男人一样弹出连接:http: //developer.android.com/guide/topics/ui/dialogs.html
像man这样的例子已经完成,但现在我无法恢复用户填写的登录名和密码。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:contentDescription="@string/logo"
android:scaleType="center"
android:src="@drawable/logo" />
<EditText
android:id="@+id/edtlogin"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/identifiant" />
<EditText
android:id="@+id/edtpassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/motdepasse"/>
</LinearLayout>
班级
public class PopUpConnexion extends DialogFragment {
private EditText login;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
// Get the layout inflater
LayoutInflater inflater = this.getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.popup_connexion, null))
// Add action buttons
.setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Here I want to recover the login and the password for sign in.
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PopUpConnexion.this.getDialog().cancel();
}
});
return builder.create();
}
}
我尝试通过各种方式恢复值,但 EditText 类型的对象登录始终为空。谢谢
编辑:
public class PopUpConnexion extends DialogFragment {
EditText login;
EditText password;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
// Get the layout inflater
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View textEntryView = inflater.inflate(R.layout.popup_connexion, null);
login = (EditText) textEntryView.findViewById(R.id.edtlogin); // with the debugger we can see it's the good EditText with the id
login.setText("test"); // we can't see "test" in the EditText
builder.setView(inflater.inflate(R.layout.popup_connexion, null))
// Add action buttons
.setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String tmp = "login: " + login.getText().toString() + "|"; // with the debugger we can see it's the good EditText with the id
Toast.makeText(getActivity().getApplication().getApplicationContext(), tmp, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PopUpConnexion.this.getDialog().cancel();
}
});
return builder.create();
}
}
使用 login.setText("test"); 我们可以得出结论,可以看到的视图与我 getText() 的视图不同。
最终编辑:
public class PopUpConnexion extends DialogFragment {
EditText identifiant;
EditText motdepasse;
Button Connexion;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
// Get the layout inflater
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View textEntryView = inflater.inflate(R.layout.popup_connexion, null);
login = (EditText) textEntryView.findViewById(R.id.edtlogin);
password = (EditText) textEntryView.findViewById(R.id.edtpassword);
Connexion = (Button) textEntryView.findViewById(R.id.loginButton);
Connexion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplication().getApplicationContext(), login.getText() + " " + password.getText(), Toast.LENGTH_SHORT).show();
}
});
builder.setView(textEntryView);
return builder.create();
}
}
最后,我不像男人那样使用事件管理器。我已经测试过这项技术,但由于在您编写 OnClickListener 时 DialogFragment.OnClickListener 和 View.OnClickListener 之间存在混淆,我遇到了一些错误。现在它可以工作了,但我认为它存在使用 builder.setPositiveButon() & co 的更好方法。