Android开发AlertDialog 简单使用方法

释放双眼,带上耳机,听听看~!

      AlertDialog对话框的应用很普及,下面我整理了一下 Dialog的简单应用  包括单选,多选,复选,自定义样式对话框等 

     所有需要注意的事项 都在注释里面标明了 这里就不啰嗦了 谈不上技术分享 激励自己坚持下去

布局文件代码如下 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:padding="4dp"
android:text="DialogDemo"
android:textSize="15sp"
android:textColor="#f0f"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_bt1"
android:layout_marginTop="15dp"
android:text="确认对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_bt2"
android:layout_marginTop="15dp"
android:text="单选对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_bt3"
android:layout_marginTop="15dp"
android:text="多选按钮对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_bt4"
android:layout_marginTop="15dp"
android:text="列表对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_bt5"
android:layout_marginTop="15dp"
android:text="自定义布局样式的对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

 

自定义对话框布局如下所示

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:src="https://blog.csdn.net/csdndouniwan/article/details/51415969/@mipmap/icon_dou"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

MainActivity 代码如下所示。

package com.imooc.www.moocdemo;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
/**
* Created by Administrator on 2016/5/15.
*/
public class DialogAcitivity extends AppCompatActivity implements View.OnClickListener {
private String sing_dialog [] = {"男","女","女博士","程序员"};//单选对话框数据源
private String mult_dialog [] = {"逗你玩","项目经理","测试","美工","程序员"};//多选对话框数据源
private String item_dialog [] = {"大领导","小领导","程序员"};//列表对话框数据源
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogactivity);
//调用初始化点击事件 界面整洁
initEvent();
}
//初始化点击数件
private void initEvent() {
//这样写点击事件直观,但是比较乱代码 还是下面的方法比较好看 哈哈
findViewById(R.id.dialog_bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog1();
}
});
findViewById(R.id.dialog_bt2).setOnClickListener(this);
findViewById(R.id.dialog_bt3).setOnClickListener(this);
findViewById(R.id.dialog_bt4).setOnClickListener(this);
findViewById(R.id.dialog_bt5).setOnClickListener(this);
}
private void showDialog1() {
//一下都是见名知意 不做过多的解释
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认对话框");
builder.setIcon(R.mipmap.ico_dw1);
builder.setMessage("确认对话框内容");
//这里注意一下Onclick的包不要导错
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击了确定按钮", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击了取消按钮", Toast.LENGTH_LONG).show();
//                dialog.dismiss();
}
});
AlertDialog dialog = builder.create();//创建Dialgo
dialog.show();//显示Dialog
}
private void showDialog2(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选按钮对话框");
builder.setIcon(R.mipmap.ico_dw1);
builder.setSingleChoiceItems(sing_dialog, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = sing_dialog[which];
Toast.makeText(getApplicationContext(),"您点击了"+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.mipmap.icon_dou);
builder.setMultiChoiceItems(mult_dialog, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(),"我喜欢做"+mult_dialog[which],Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"我不喜欢做"+mult_dialog[which],Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void showDialog4(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("部门列表对话框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setItems(item_dialog, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"我不喜欢做"+item_dialog[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.dlaloglayout,null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setTitle("则是个自定义布局");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"点击了自定义对话框的确认按钮",Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_bt2:
showDialog2();
//                Toast.makeText(getApplicationContext(),"电解了",Toast.LENGTH_LONG).show();
break;
case R.id.dialog_bt3:
showDialog3();
break;
case R.id.dialog_bt4:
showDialog4();
break;
case R.id.dialog_bt5:
showDialog5();
break;
}
}
}

人已赞赏
Android文章

Android发送一条普通广播 有序广播 Broadcast 和 Receiver

2020-3-27 20:16:48

Android文章

Android开发CharSequence与String的区别

2020-3-27 21:31:58

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索