在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" >

<Button

android:id="@+id/mybut"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="用户登录"/>

</LinearLayout>

在MyDialogDemo.java程序中

package com.tarena.dialog;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.TextView;

public class MyDialogDemo extends Activity {

private Button mybut = null ; // 定义按钮

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main); // 调用布局管理器

this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮

this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类

}

private class OnClickListenerImpl implements OnClickListener {

public void onClick(View view) {

LayoutInflater facotry = LayoutInflater.from(MyDialogDemo.this); //相当于界面的创建工厂

View myview = facotry.inflate(R.layout.login, null); //读取配置文件

Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)

.setIcon(R.drawable.ic_launcher)

.setTitle("用户登录")

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

public void onClick(DialogInterface dialog, int which) {

}

})

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

public void onClick(DialogInterface dialog, int which) {

}

}).setView(myview).create() ; // 设置对话框

dialog.show() ;

}

}

}

在layout文件夹中新建login.xml

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TableRow >

<TextView

android:text="用户名:"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"/>

<EditText

android:layout_width="150dp"

android:layout_height="wrap_content"/>

</TableRow>

<TableRow >

<TextView

android:text="密码:"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"/>

<EditText

android:password="true"

android:layout_width="150dp"

android:layout_height="wrap_content"/>

</TableRow>

</TableLayout>

更多相关文章

  1. android 设置静态wifi地址
  2. Android(安卓)studio实现按钮的点击事件
  3. Android模仿Buttonbar
  4. Android(安卓)目录操作
  5. Android(安卓)一个简单的计算器APP
  6. android项目colors.xml中几个常用的颜色资源
  7. android Buton使用
  8. 2013.12.04 (3)——— android SlidingMenu之PropertiesActivity
  9. Android之AlertDialog Demo(20190115)

随机推荐

  1. android Setting中隐藏项实现原理与代码
  2. Android真的很火吗?
  3. Android自定义视图一:扩展现有的视图,添加
  4. Android将允许纯C/C++开发应用
  5. 最封闭的开源系统:话说 Android(安卓)的八
  6. 给小白分享几个学习Android的网站
  7. Android内核开发:图解Android系统的启动过
  8. Android下的单元测试
  9. Android中的MVP架构初探
  10. Android将允许纯C/C++开发应用