Android 如何在类似DialogFrament的弹出窗口中恢复参数
我正在尝试弹出一个连接,就像这个男人: 像这个人一样的例子已经完成了,但是现在我无法恢复用户的登录名和密码 布局:Android 如何在类似DialogFrament的弹出窗口中恢复参数,android,android-layout,popup,android-alertdialog,android-dialogfragment,Android,Android Layout,Popup,Android Alertdialog,Android Dialogfragment,我正在尝试弹出一个连接,就像这个男人: 像这个人一样的例子已经完成了,但是现在我无法恢复用户的登录名和密码 布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"
<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>
我试图恢复这些值无论如何,EditText类型的对象登录总是空的。
谢谢
编辑:
使用login.setText(“test”);我们可以得出结论,可以看到的视图与我获取文本()的视图不同
最终编辑:
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是一种更好的方法。您希望首先显示对话框。你想要的那种类型我不明白什么是“frnd”,但我想做点什么链接:好的,你只想显示弹出窗口,意思是使用Dialog videoDialog=newdialog(YouActivityname.this);videoDialog.requestWindowFeature(Window.FEATURE\u NO\u TITLE);videoDialog.setContentView(R.layout.opecofabpopup);videoDialog.setCanceledOnTouchOutside(假);videoDialog.show();我已经到达显示弹出窗口,我只想恢复弹出窗口中EditText的值。我编辑了第一条消息以显示我想要的数据。问题是我从未登录过,登录总是空的:login=(EditText)(inflater.findViewById(R.id.edtlogin));-->null/login=(EditText)this.getActivity().findViewById(R.id.edtlogin);-->null/login=(EditText)this.getDialog().findViewById(R.id.edtlogin);-->无效的但是我发现了一些东西:final View texentryview=inflater.inflate(R.layout.popup\u connexion,null);login=(EditText)textEntryView.findViewById(R.id.edtlogin);-->它不是null,但login.getText()返回一个空字符串。您可以使用textEntryView.login.getText()。试试这个。
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();
}
}
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();
}
}