Android对话框实例-注册对话框

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">
    
    <Button android:id="@+id/mybut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="注册"/>

</LinearLayout>

login.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    
    <TableRow android:gravity="center_horizontal">
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:textSize="16dp"/>
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="填写登录账号"
            android:selectAllOnFocus="true"/>
        
    </TableRow>
    
    <TableRow android:gravity="center_horizontal">
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="16dp"/>
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true"/>
        
    </TableRow>

    <TableRow android:gravity="center_horizontal">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话"
            android:textSize="16dp"/>
        
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入电话号码"
            android:selectAllOnFocus="true"
            android:phoneNumber="true"
            
            />
    </TableRow>
    
    <TableRow android:gravity="center_horizontal"
        android:layout_marginTop="10dp">
        
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="点  击  注  册"/>
        
    </TableRow>
    
</TableLayout>


Main.java

package com.example.logindialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;


public class MainActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Builder b=new AlertDialog.Builder(this);

Button but=(Button) super.findViewById(R.id.mybut);

but.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
b.setIcon(R.drawable.ic_launcher);
b.setTitle("注册账户");
//getLayoutInflater()是用来找Layout下的xml文件,对于没有载入或者需要动态载入的需要inflate
TableLayout loginForm=(TableLayout) getLayoutInflater().inflate(R.layout.login, null);
//设置对话框显示的View为loginForm
b.setView(loginForm);
//为对话框设置一个登录按钮

b.setPositiveButton("登录", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 此处可以执行点击登录按钮之后的操作

}
});

b.setNegativeButton("取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮的操作可以在这里处理

}
});

b.create().show();

}
});

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值