Android对话框
来源:互联网 发布:ec20 linux驱动 编辑:程序博客网 时间:2024/05/12 13:06
使用Activity中的onCreateDialog()方法写一个对话框
先写布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.dada.cxl.createdialogtest.MainActivity"><Button android:id="@+id/btn_dialog" android:text="show dialog" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
java文件编写的代码
package com.dada.cxl.createdialogtest;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { private Button btn_dialog; CharSequence[] items = {"Goocle","Apple","Microsoft"}; boolean[] itemsChecked = new boolean[items.length]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_dialog = (Button) findViewById(R.id.btn_dialog); btn_dialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { switch(id){ case 0: return new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher) .setTitle("this is a dialog with some simple text...") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"ok clicked!",Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"cancel clicked!",Toast.LENGTH_SHORT).show(); } }) .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this,items[which]+(isChecked?"checked!":"unchecked"),Toast.LENGTH_SHORT).show(); } }).create(); } return null; }}
当然还有其他格式的Dialog,可以自己去尝试一下
0 0
- Android对话框
- Android对话框
- android 对话框
- android 对话框
- Android对话框
- Android对话框
- Android对话框
- Android对话框
- android对话框
- Android对话框
- android对话框
- Android对话框
- Android对话框
- android对话框
- android对话框
- Android对话框
- android 对话框
- android 对话框
- php环境配置
- OpenCV 源码解读——createsample
- C++ 用两个栈实现一个队列
- 错误提示:transform.position assign attempt for 'XXX' is notvalid。Input rotation is {NaN, NaN, NaN, NaN}
- Android中几种Button的监听方法
- Android对话框
- Exception in thread "main" org.hibernate.MappingException: invalid configuration
- jmeter测试TCP服务器/模拟发送TCP请求
- 编程技巧系列(5)字符串格式化常用做法
- Callback机制_实现(2)
- 第十八课程序容错能力——C#计算器编程教学
- 定量实验的误差评价指标选择
- spring data jpa batch批处理
- 实现一个noncopyable