我一直在使用AlertDialog类来显示自定义对话框(forgot_password.xml),然后从自定义对话框接收输入。为了从edittext中获取输入,我使用了layoutinflator,但它没有从字段中接收任何内容。我使用toast消息来验证用户输入。另外还有一个Dialog类我们可以使用dismiss()来关闭Dialog,类似于AlertDialog我要做什么?
以下是我的forgot_password.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center_vertical"
android:text="@string/tv_3rd_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<EditText
android:id="@+id/et_3rd_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_edittext"
android:ems="10"
android:hint="@string/et_2nd_hint_name"
android:paddingLeft="20dp" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:background="@null"
android:contentDescription="@string/ib_delete_text"
android:onClick="onClick"
android:src="@drawable/cancel_circle" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp" >
<EditText
android:id="@+id/et_3rd_emailid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_edittext"
android:ems="10"
android:hint="@string/et_2nd_hint_email"
android:paddingLeft="20dp" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:background="@null"
android:contentDescription="@string/ib_delete_text"
android:onClick="onClick"
android:src="@drawable/cancel_circle" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginBottom="5dp" >
<Button
android:id="@+id/btn_3rd_close"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="60dp"
android:background="@drawable/button_states"
android:text="@string/btn_1st_exit" />
<Button
android:id="@+id/btn_3rd_send"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:background="@drawable/button_states"
android:text="@string/btn_3rd_send" />
</FrameLayout>
</LinearLayout>
以下是我的AlertDialog编码在切换案例中的MainActivity的onCreateDialog()中使用它
protected Dialog onCreateDialog(int id, Bundle args) {
Dialog d = null;
switch(id){
case R.id.ib_1st_forgot:
AlertDialog.Builder ab = new AlertDialog.Builder(this);
//used layout inflator to fetch user input from forgot_password.xml
LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.forgot_password, null);
ab.setView(ll);
d = ab.create();
//forgot_password.xml file's buttons and edittext
Button send_link = (Button) ll.findViewById(R.id.btn_3rd_send);
Button close_dlg = (Button) ll.findViewById(R.id.btn_3rd_close);
EditText e_name = (EditText) ll.findViewById(R.id.et_3rd_username);
EditText e_email = (EditText) ll.findViewById(R.id.et_3rd_emailid);
final String names = e_name.getText().toString();
final String emails = e_email.getText().toString();
send_link.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "username is "+names, 0).show();
}
});
close_dlg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//need a dialog close operation here
}
});
break;
}
return d;
}
答案 0 :(得分:0)
问题是以下两行的位置:
final String names = e_name.getText().toString();
final String emails = e_email.getText().toString();
他们在充实意见后立即执行,这没有多大意义。相反,你可能想要的是在其中一个OnClickListener
被击中时获取这些值。所以基本上把它们移到onClick()
:
final EditText e_name = (EditText) ll.findViewById(R.id.et_3rd_username);
final EditText e_email = (EditText) ll.findViewById(R.id.et_3rd_emailid);
send_link.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String names = e_name.getText().toString();
String emails = e_email.getText().toString();
Toast.makeText(getApplicationContext(), "username is "+names, 0).show();
}
});
另外还有一个Dialog类我们可以使用dismiss()来关闭Dialog,类似于AlertDialog
是的,当然。只需致电d.dismiss()
即可手动关闭您正在创建的对话框。