android 定制对话框 Layoutlnflater_第1张图片

在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 studio实现按钮的点击事件
  2. Android实现“退出确认”对话框
  3. 如何避免输入法弹出时遮挡住按钮或输入框
  4. Android 按钮 弹出对话框
  5. Android - 对话框(Dialog)和通知(Notification)2
  6. Android中对话框的使用
  7. Android对话框的几种形式
  8. android 弹出带按钮的对话框

随机推荐

  1. Android之ButterKnife用法详解
  2. 【Android】 Painless Thread
  3. 安卓手机恶意代码——Samsapo
  4. 《第一行代码--Android》读书笔记之内容
  5. Android MediaPlayer音频播放器详解
  6. 实现对特定内容改变字体颜色的TextView
  7. android自定义按钮控件样式
  8. ubuntu下eclipse Android(安卓)ADT中SDK
  9. 回忆录——国庆
  10. Android 基础知识介绍