我寻求了许多答案/类似的问题,但没有解决我的问题。我有一个在扩展"对话片段"的类中实现的自定义对话框。当我尝试从任何布局组件获取文本时,我会得到我坐的初始默认文本。
代码片段:
public class input1_frag extends DialogFragment
{
View v;
LayoutInflater inflater;
EditText email_field,passwd_field;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setTitle("Identity verification");
// Inflate and set the layout for the dialog
builder.setView(inflater.inflate(R.layout.input_dialog, null));
builder.setPositiveButton("Modify account", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
v = inflater.inflate(R.layout.input_dialog, null);
email_field = (EditText) v.findViewById(R.id.input_dialog_email0);
passwd_field = (EditText) v.findViewById(R.id.input_dialog_passwd0);
String fetched_email = email_field.getText().toString();
String fetched_passwd = passwd_field.getText().toString();
});}}
输入对话框.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your old data"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow android:id="@+id/tableRow_space_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View android:layout_width="fill_parent"
android:layout_height="20dp">
</View>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input_dialog_email0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="E-mail" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/input_dialog_passwd0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="xxx" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
</TableLayout>
我把视图和膨胀全局化,以避免使它们成为最终的。但它仍然从文本字段返回初始文本。
从onClick
方法中删除以下行并在builder.setView()
中使用它。因此,线条视图将重置为其原始形式,并且对编辑文本所做的更改将丢失。
v = inflater.inflate(R.layout.input_dialog, null);
喜欢这个
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setTitle("Identity verification");
// Inflate and set the layout for the dialog
v = inflater.inflate(R.layout.input_dialog, null);
builder.setView(v);
builder.setPositiveButton("Modify account", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
email_field =
...
...