登入对话框

/*
 * 登入对话框
 * 在xml文件里我们定义了三个输入框,一个是用户名输入框、
 * 一个是密码输入框另一个是电话号码输入框,下面我们就在
 * 应用程序中调用AlertDialog.Builder的setView(View view)
 * 方法让对话框显示该输入界面
 */

import 略

public class Ex02_11Activity extends Activity {
	private Button bt;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		bt = (Button) findViewById(R.id.bt);
		final Builder builder = new AlertDialog.Builder(this);
		bt.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stu
				TableLayout loginForm = (TableLayout) getLayoutInflater()
						.inflate(R.layout.loginlayout, null);
				// 设置对话框图标和标题
				builder.setIcon(R.drawable.tools)
						.setTitle("自定义对话框")
						.setView(loginForm)//这里是为对话框添加布局文件
						.setPositiveButton("登入", new OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// 此处填写点 击登 入按钮后处理的事件
							}
						})
						.setNeutralButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										// 此处填写点击 取消 按钮后处理的事件
									}
								}).show();
			}
		});
	}
}

下面我们就来看看两个布局文件main.xml和loginlayout.xml。
main.xml文件其实很简单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal|center"
        android:text="@string/hello"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="@string/regist" />

</LinearLayout>

loginlayout.xml文件code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loginForm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TableRow>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <!-- 输入用户名文本框 -->

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hintUser"
            android:selectAllOnFocus="true" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <!-- 输入密码文本框 -->

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hintPwd"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/phone" />

        <!-- 输入电话号码文本框 -->

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:selectAllOnFocus="true" />
    </TableRow>

</TableLayout>


下面我们来看下程序运行后的效果:

完..............................................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值