对话框小程序
2016-03-21 09:18
489 查看
MainActivity
package com.example.dialogdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] single_list = {"男","女","女博士","程序员"};
String[] multi_list = {"男","女","女博士","程序员"};
String[] items_list = {"项目经理","美工","测试","程序员"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initEvent();
}
/**
* 初始化点击事件
*/
private void initEvent(){
findViewById(R.id.bt_click).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// showDialog1();
// showDialog2();
// showDialog3();
// showDialog4();
showDialog5();
}
});
}
/**
* 显示确认对话框
*/
private void showDialog1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认对话框");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("确认对话框提示内容");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示单选对话框
*/
private void showDialog2(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择性别");
builder.setIcon(R.drawable.ic_launcher);
builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String str = single_list[which];
Toast.makeText(MainActivity.this, "这个人是"+str, Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示多选对话框
*/
private void showDialog3(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱好");
builder.setIcon(R.drawable.ic_launcher);
builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "我喜欢"+multi_list[which], Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "我不喜欢"+multi_list[which], Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示列表对话框
*/
private void showDialog4(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("部门列表");
builder.setIcon(R.drawable.ic_launcher);
builder.setItems(items_list, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我动了"+items_list[which], Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示自定义对话框
*/
private void showDialog5(){
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("自定义对话框");//设置标题
builder.setIcon(R.drawable.ic_launcher);//设置图标
builder.setView(view);
AlertDialog dialog = builder.create();//获取dialog
dialog.show();//显示对话框
}
}
activity_main.xml
<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=".MainActivity" >
<Button
android:id="@+id/bt_click"
android:layout_width="150dip"
android:layout_height="40dip"
android:text="dialog测试"/>
</RelativeLayout>
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="输入内容。。"
android:layout_weight="1"
android:id="@+id/et_input"/>
<Button
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
package com.example.dialogdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] single_list = {"男","女","女博士","程序员"};
String[] multi_list = {"男","女","女博士","程序员"};
String[] items_list = {"项目经理","美工","测试","程序员"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initEvent();
}
/**
* 初始化点击事件
*/
private void initEvent(){
findViewById(R.id.bt_click).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// showDialog1();
// showDialog2();
// showDialog3();
// showDialog4();
showDialog5();
}
});
}
/**
* 显示确认对话框
*/
private void showDialog1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认对话框");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("确认对话框提示内容");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示单选对话框
*/
private void showDialog2(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择性别");
builder.setIcon(R.drawable.ic_launcher);
builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String str = single_list[which];
Toast.makeText(MainActivity.this, "这个人是"+str, Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示多选对话框
*/
private void showDialog3(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱好");
builder.setIcon(R.drawable.ic_launcher);
builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "我喜欢"+multi_list[which], Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "我不喜欢"+multi_list[which], Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示列表对话框
*/
private void showDialog4(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("部门列表");
builder.setIcon(R.drawable.ic_launcher);
builder.setItems(items_list, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我动了"+items_list[which], Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* 显示自定义对话框
*/
private void showDialog5(){
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("自定义对话框");//设置标题
builder.setIcon(R.drawable.ic_launcher);//设置图标
builder.setView(view);
AlertDialog dialog = builder.create();//获取dialog
dialog.show();//显示对话框
}
}
activity_main.xml
<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=".MainActivity" >
<Button
android:id="@+id/bt_click"
android:layout_width="150dip"
android:layout_height="40dip"
android:text="dialog测试"/>
</RelativeLayout>
dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="输入内容。。"
android:layout_weight="1"
android:id="@+id/et_input"/>
<Button
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
相关文章推荐
- 微信退款demo--Java
- 当OA遇到微信,通达OA官方微信公众号来啦!
- 微信登入,获取用户信息
- QQ也可以和微信一样只能共同好友见评论
- 微信公众平台开发(一) 配置接口
- 小程序分析
- [编程题]微信红包
- 我运营公众号这一个月
- 微信开发01-申请新浪SAE
- 微信开发环境搭建
- 微信图片反防盗链的方法(此图片来自微信公众平台,未经允许不可引用)
- 第一个小程序:用户登录
- 【2016.3.19】作业 分析一个很有(wu)趣(liao)的小程序
- 初始化微信企业号秘钥
- 微信jsApiPay支付踩过的坑
- 微信jsapi invalid signature错误
- QQ、微信、新浪等开放平台网址(申请接入)
- 阅读微信支付demo收获
- 微信红包实现原理[转]
- 微信支付类 demo