Android普通对话框用法实例分析
更新时间:2015年09月16日 12:06:53 作者:Ruthless
这篇文章主要介绍了Android普通对话框用法,以实例形式较为详细的分析了Android对话框的创建技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Android普通对话框用法。分享给大家供大家参考。具体如下:
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:text="" android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <Button android:text="显示普通对话框" android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
AlertDialog类:
package com.ljq.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AlertDialog extends Activity {
private EditText editText;
private final static int DIALOG=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText)findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 显示对话框
showDialog(DIALOG);
}
});
}
/**
* 创建普通对话框
*/
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog=null;
switch (id) {
case DIALOG:
Builder builder=new android.app.AlertDialog.Builder(this);
//设置对话框的图标
builder.setIcon(R.drawable.header);
//设置对话框的标题
builder.setTitle("普通对话框");
//设置对话框的显示内容
builder.setMessage("这是普通对话框中的内容!!");
//添加按钮,android.content.DialogInterface.OnClickListener.OnClickListener
builder.setPositiveButton(" 确 定 ", new OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
editText.setText("这是普通对话框中的内容!!");
}
});
//创建一个普通对话框
dialog=builder.create();
break;
}
return dialog;
}
}
运行结果:

希望本文所述对大家的Android程序设计有所帮助。
相关文章
Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
这篇文章主要介绍了Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法,结合实例形式分析了Android的经纬度地址解析与json格式数据操作相关技巧,需要的朋友可以参考下2017-02-02
使用AndroidStudio上传忽略文件至SVN Server的解决办法
这篇文章主要介绍了使用AndroidStudio上传忽略文件至SVN Server的解决办法 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-06-06
Android使用原生组件WebView加载网页和数据的方法
这篇文章主要介绍了Android使用原生组件WebView加载网页和数据的方法的相关资料,需要的朋友可以参考下2016-09-09
Android Volley扩展实现支持进度条的文件上传功能
这篇文章主要为大家详细介绍了Android Volley扩展实现文件上传与下载功能,支持进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-12-12


最新评论