如何从另一个布局访问 AlertDialog Builder 的 EditText 值?
How to acces EditText value from AlertDialog Builder from another layout?
我的配置 activity 中有这段代码,当我按下 add_count TextView 时对话框被激活,我的问题似乎是我在 get_cont 上获得的值是null,也许是因为充气器不能正常工作?
(用户名和密码是ArrayLists)
add_cont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
LayoutInflater inflater = Config.this.getLayoutInflater();
final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
builder.setTitle("Add cont.");
builder.setView(inflater.inflate(R.layout.dialog_add_cont, null))
.setPositiveButton(R.string.create_cont, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText get_cont = (EditText)dialog_layout.findViewById(R.id.username);
EditText get_pass = (EditText)dialog_layout.findViewById(R.id.password);
String test_cont = get_cont.getText().toString();
String text_pass = getpass.getText().toString();
usernames.add(3, get_cont.getText().toString());
passwords.add(3, get_pass.getText().toString());
}
})
.setNegativeButton(R.string.cancel_cont, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
我的 dialog_add_cont 布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
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="username" />
<EditText
android:id="@+id/password"
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="password"/>
我是不是把密码弄错了?
您正在膨胀 dialog_layout 但从未使用它,因为您正在再次膨胀 xml 并直接为对话框设置它。
你的代码应该是这样的:
AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
LayoutInflater inflater = Configurare.this.getLayoutInflater();
final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
builder.setTitle("Add cont.");
builder.setView(dialog_layout)
....
你试过这样做吗?
@Override
public void onClick(DialogInterface dialog, int id) {
Dialog d = (Dialog) dialog;
EditText get_cont = (EditText)d.findViewById(R.id.username);
EditText get_pass = (EditText)d.findViewById(R.id.password);
String test_cont = get_cont.getText().toString();
String text_pass = get_cont.getText().toString();
usernames.add(3, get_cont.getText().toString());
passwords.add(3, get_pass.getText().toString());
}
我的配置 activity 中有这段代码,当我按下 add_count TextView 时对话框被激活,我的问题似乎是我在 get_cont 上获得的值是null,也许是因为充气器不能正常工作? (用户名和密码是ArrayLists)
add_cont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
LayoutInflater inflater = Config.this.getLayoutInflater();
final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
builder.setTitle("Add cont.");
builder.setView(inflater.inflate(R.layout.dialog_add_cont, null))
.setPositiveButton(R.string.create_cont, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText get_cont = (EditText)dialog_layout.findViewById(R.id.username);
EditText get_pass = (EditText)dialog_layout.findViewById(R.id.password);
String test_cont = get_cont.getText().toString();
String text_pass = getpass.getText().toString();
usernames.add(3, get_cont.getText().toString());
passwords.add(3, get_pass.getText().toString());
}
})
.setNegativeButton(R.string.cancel_cont, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
我的 dialog_add_cont 布局如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
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="username" />
<EditText
android:id="@+id/password"
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="password"/>
我是不是把密码弄错了?
您正在膨胀 dialog_layout 但从未使用它,因为您正在再次膨胀 xml 并直接为对话框设置它。
你的代码应该是这样的:
AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
LayoutInflater inflater = Configurare.this.getLayoutInflater();
final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
builder.setTitle("Add cont.");
builder.setView(dialog_layout)
....
你试过这样做吗?
@Override
public void onClick(DialogInterface dialog, int id) {
Dialog d = (Dialog) dialog;
EditText get_cont = (EditText)d.findViewById(R.id.username);
EditText get_pass = (EditText)d.findViewById(R.id.password);
String test_cont = get_cont.getText().toString();
String text_pass = get_cont.getText().toString();
usernames.add(3, get_cont.getText().toString());
passwords.add(3, get_pass.getText().toString());
}