`

Android2.2中在AlertDialog对话框中添加EditText文本框

阅读更多

在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中,作为住页面其中的一个组成部分。


运行结果示意图:

 

 

---------------------------------------------------------------------------------

以上内容为个人理解总结...

2
3
分享到:
评论
4 楼 wjfyulong 2012-06-13  
不知怎么解决
3 楼 wjfyulong 2012-06-13  
用scrollView 会有点问题呀
2 楼 wjfyulong 2012-06-13  
要是editText有5个到6个怎么办呢
1 楼 zhidde 2012-03-16  
简单却有所收获,多谢分享

相关推荐

Global site tag (gtag.js) - Google Analytics