在Android开发中,总会用到使用AlertDialog+EditText组件呈现给用户,让用户填写一些信息,那么在AlertDialog对话框中如何添加出EditText文本框组件呢,这里我自己总结了一种方法来实现:
在这里,我们想实现的逻辑是:点击"我要登录"按钮,然后再当前页面中弹出一个对话框,对话框中是让用户填写登录的账号与密码,然后点击"登录"按钮后在当前页面中显示用户输入的账号与密码信息。
创建主配置文件main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<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"?>
<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中的代码:
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 {
/** Called when the activity is first created. */
@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) {
/**获取引用edittext.xml配置文件中的视图组件*/
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.edittext, null);
/**这里使用链式写法创建了一个AlertDialog对话框,并且把应用到得视图viwe放入到其中*/
/**添加AlertDialog的用户登录按钮,并且设置按钮响应事件*/
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中,作为住页面其中的一个组成部分。
运行结果示意图:
---------------------------------------------------------------------------------
以上内容为个人理解总结...
分享到:
相关推荐
Android 自定义实现一个AlertDialog对话框,提示框或警告框弹出式对话框,套用了系统Holo风格,生成符合系统主题的AlertDialog.Builder,可以分别定义弹出提示的标题文字及提示内容: builder.setTitle("Test ...
Android AlertDialog对话框 自定义实现遮罩层
Android源码——alertDialog对话框.zip
Android alertDialog对话框.zip源码资源下载Android alertDialog对话框.zip源码资源下载
Android之修改AlertDialog对话框及使用系统Holo风格
android AlertDialog对话框的类型集合 ,比较全面的讲述了 AlertDialog 的 种类 , Demo 的综合性比较强 , Demo 可直接运行
本文实例讲述了Android编程实现在自定义对话框中获取EditText中数据的方法。分享给大家供大家参考,具体如下: 在项目中忽然遇到这样的问题,需要自定义对话框,对话框需要有一个输入框,以便修改所选中的价格,然后...
安卓Android源码——alertDialog对话框.zip
android 显示 AlterDialog
Android alertDialog对话框.rar
Android alertDialog对话框,实现多种对话框模式
Android alertDialog对话框.zip项目安卓应用源码下载Android alertDialog对话框.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
【Android】对话框 AlertDialog
自定义AlertDialog对话框用于加载等耗时任务提示,博文地址http://blog.csdn.net/u012806692,在这个地址的内容中找下,因为刚写完还是404。。。。
android 使用alertdialog创建自定义对话框
本文实例讲述了Android AlertDialog对话框用法。分享给大家供大家参考,具体如下: AlertDialog对话框的介绍 1、获得AlertDialog静态内部类Buidler对象,由该类来创建AlertDialog对象,因为AlertDialog的构造方法全部...
一个简单的自定义AlertDialog