Android中简单提示对话框
来源:互联网 发布:mysql服务 启动 linux 编辑:程序博客网 时间:2024/05/16 13:50
1. ShowDialog.java
package com.ShowDialog;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ShowDialog extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mybutton = (Button)findViewById(R.id.button1);
mybutton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {//单击此按钮后显示对话框。对话框的布局定义在res\dialogshow.xml中
LayoutInflater usingdialoglayoutxml = LayoutInflater.from(ShowDialog.this);
final View myviewondialog = usingdialoglayoutxml.inflate(R.layout.dialogshow, null);//按照设定的布局显示对话框
AlertDialog mydialoginstance = new AlertDialog.Builder(ShowDialog.this)
.setIcon(R.drawable.icon)//图标,显示在对话框标题左侧
.setTitle("用户登录界面")//对话框标题
.setView(myviewondialog)//注意参数为上面定义的View实例名,意为显示R.layout.dialogshow.xml这个布局文件
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getApplicationContext(), "单击了确定按钮", Toast.LENGTH_LONG).show();
}})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ShowDialog.this.finish();//退出程序
}})
.create();
mydialoginstance.show(); //显示对话框
}
});
}
}
2.自定义的对话框布局文件dialogshow.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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/username_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="用户名"
android:gravity="left"/>
<EditText
android:id="@+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
/>
<TextView
android:id="@+id/password_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="密码"
android:gravity="left"/>
<EditText
android:id="@+id/password_edit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true" />
</LinearLayout>
3.运行之后的效果
注意:LayoutInflater类的作用类似于findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件并且实例化,而findViewById()是找具体xml下具体widget控件
- Android中简单提示对话框
- Android 提示对话框
- Android中简单列表对话框问题
- 常用的android提示对话框
- 常用的android提示对话框
- android 提示对话框显示 AlertDialog
- android对话框和提示框
- Android系统提示对话框实现
- Android 简单对话框
- Android 对话框简单写法
- Android对话框简单例子
- Android中简单仿iPhone的Dialog对话框
- Android项目中简单易用的加载对话框
- Android简单对话框、输入对话框、单选对话框、复选对话框、列表对话框、自定义对话框、时间对话框
- Android简单提示框
- android中自定义的一个可以弹出类似于提示更新的对话框
- Android中判断应用运行时是否有网络连接并弹出对话框提示
- Android消息提示框和对话框
- 字符串的完美度
- 51+DS18B20测量温度(续)
- SPOJ 351. Ha-noi! 汉诺塔递归模拟
- Direct2D 几何计算和几何变幻
- HDU To The Max
- Android中简单提示对话框
- 机器大小端判断
- SQL语句的复制
- SQL定期自动清理数据库日志文件
- oracle汉字排序问题
- JQuery实现鼠标移上弹出提示框,移出消失
- 删除^M的两种方法
- centOS中文乱码解决
- 关于中文字符显示为乱码