在Android开发中,总会用到使用AlertDialog+EditText组件呈现给用户,让用户填写一些信息,那么在AlertDialog对话框中如何添加出EditText文本框组件呢,这里我自己总结了一种方法来实现:
在这里,我们想实现的逻辑是:点击"我要登录"按钮,然后再当前页面中弹出一个对话框,对话框中是让用户填写登录的账号与密码,然后点击"登录"按钮后在当前页面中显示用户输入的账号与密码信息。
创建主配置文件main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
Java代码
- <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" -
android:orientation="vertical" -
android:layout_width="fill_parent" -
android:layout_height="fill_parent" -
> -
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> -
<Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="match_parent" android:text="@string/login"></Button> -
</LinearLayout> - </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="match_parent" android:text="@string/login"></Button> </LinearLayout> </LinearLayout>
创建登录使用的配置文件edittext.xml
<?xml version="1.0" encoding="utf-8"?>
Java代码
- <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" -
android:orientation="vertical" -
android:layout_width="fill_parent" -
android:layout_height="fill_parent" -
> -
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1"> -
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip"> -
<TextView android:id="@+id/textView1" android:text="@string/name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip"></TextView> -
<EditText android:layout_weight="1" android:text="请输入用户名" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> -
</TableRow> -
</TableLayout> -
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableRow2" android:clickable="true" android:padding="10dip"> -
<TextView android:id="@+id/textView2" android:text="@string/pass" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip"></TextView> -
<EditText android:layout_weight="1" android:text="请输入密码" android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> -
</TableRow> - </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip"> <TextView android:id="@+id/textView1" android:text="@string/name" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip"></TextView> <EditText android:layout_weight="1" android:text="请输入用户名" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> </TableRow> </TableLayout> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableRow2" android:clickable="true" android:padding="10dip"> <TextView android:id="@+id/textView2" android:text="@string/pass" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip"></TextView> <EditText android:layout_weight="1" android:text="请输入密码" android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> </TableRow> </LinearLayout>
在类MainActivity中的代码:
Java代码
- package
cn.android.activity; -
- import
android.app.Activity; - import
android.app.AlertDialog; - import
android.content.DialogInterface; - import
android.os.Bundle; - import
android.view.LayoutInflater; - import
android.view.View; - import
android.view.View.OnClickListener; - import
android.widget.Button; - import
android.widget.EditText; - import
android.widget.Toast; -
- public
class MainActivity extends Activity implements OnClickListener { -
-
@Override -
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
-
Button button1 = (Button) this.findViewById(R.id.button1); -
button1.setOnClickListener(this); -
} -
-
@Override -
public void onClick(View arg0) { -
-
<SPAN style="COLOR: #ff0000"> -
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE); -
final View view = inflater.inflate(R.layout.edittext, null);</SPAN> -
-
-
-
-
new AlertDialog.Builder(MainActivity.this).setTitle("用户登录").<SPAN style="COLOR: #ff0000">setView(view)</SPAN>.setPositiveButton("登录",new DialogInterface.OnClickListener() { -
-
@Override -
public void onClick(DialogInterface arg0, int arg1) { -
<SPAN style="COLOR: #ff0000"> -
EditText nameEditText = (EditText) view.findViewById(R.id.editText1); -
-
String username = nameEditText.getText().toString(); -
-
-
EditText passEditText = (EditText) view.findViewById(R.id.editText2); -
-
String userpass = passEditText.getText().toString();</SPAN> -
-
-
Toast.makeText(MainActivity.this, "您的用户名是:"+username+","+"您的密码是:"+userpass, Toast.LENGTH_LONG).show(); -
} -
-
}).setNegativeButton("退出", new DialogInterface.OnClickListener() { -
-
@Override -
public void onClick(DialogInterface arg0, int arg1) { -
Toast.makeText(MainActivity.this, "您取消了登录", Toast.LENGTH_LONG).show(); -
} -
}).show(); -
} - }
package cn.android.activity; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1 = (Button) this.findViewById(R.id.button1); button1.setOnClickListener(this); } @Override public void onClick(View arg0) { LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE); final View view = inflater.inflate(R.layout.edittext, null); new AlertDialog.Builder(MainActivity.this).setTitle("用户登录").setView(view).setPositiveButton("登录",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { EditText nameEditText = (EditText) view.findViewById(R.id.editText1); String username = nameEditText.getText().toString(); EditText passEditText = (EditText) view.findViewById(R.id.editText2); String userpass = passEditText.getText().toString(); Toast.makeText(MainActivity.this, "您的用户名是:"+username+","+"您的密码是:"+userpass, Toast.LENGTH_LONG).show(); } }).setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "您取消了登录", Toast.LENGTH_LONG).show(); } }).show(); } }
这里我们用到的方法是把其他配置文件中的视图引入到当前视图的 AlertDialog中,作为住页面其中的一个组成部分。
运行结果示意图: