zoukankan      html  css  js  c++  java
  • AlertDialog的实现

    课程Demo

    重点解析自定义对话框

    public class MainActivity extends AppCompatActivity {
    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button testbt;
    private EditText edit;
    private TextView text1;
    private String singleitem[]={"男","女","人妖"};
    private String moreitem[]={"杨幂","刘诗诗","宋茜","古力娜扎"};
    private String listitem[]={"杨幂","刘诗诗","宋茜","古力娜扎"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt1= (Button) findViewById(R.id.bt1);
    bt2= (Button) findViewById(R.id.bt2);
    bt3= (Button) findViewById(R.id.bt3);
    bt4= (Button) findViewById(R.id.bt4);
    bt5= (Button) findViewById(R.id.bt5);
    showDialog1();
    showDialog2();
    showDialog3();
    showDialog4();
    showDialog5();
    }
    //普通对话框的实现
    private void showDialog1() {
    bt1.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder build=new AlertDialog.Builder(MainActivity.this);
    @Override
    public void onClick(View v) {
    build.setTitle("确认对话框");
    build.setIcon(R.drawable.she);
    build.setMessage("是否确定你的选择");
    build.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show();
    }
    });
    build.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
    dialog.dismiss();
    }
    });
    AlertDialog dialog=build.create();
    dialog.show();
    }
    });
    }
    //单选对话框的实现
    private void showDialog2() {
    bt2.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder build=new AlertDialog.Builder(MainActivity.this);
    @Override
    public void onClick(View v) {
    build.setIcon(R.drawable.she);
    build.setTitle("你的性别是:");
    //singleitem:存放数据的数据名 0:默认选择第一个
    build.setSingleChoiceItems(singleitem, 0, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this,"你的性别为"+singleitem[which],Toast.LENGTH_SHORT).show();
    }
    });
    AlertDialog dialog=build.create();
    dialog.show();
    }
    });
    }
    //多选对话框的实现
    private void showDialog3() {
    bt3.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
    @Override
    public void onClick(View v) {
    build.setTitle("多选对话框");
    build.setIcon(R.drawable.she);
    build.setTitle("你喜欢的人是:");
    build.setMultiChoiceItems(moreitem, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (isChecked) {
    Toast.makeText(MainActivity.this, "你喜欢:" + moreitem[which], Toast.LENGTH_SHORT).show();
    }
    else{
    Toast.makeText(MainActivity.this, "你不喜欢:" + moreitem[which], Toast.LENGTH_SHORT).show();
    }
    }
    });
    AlertDialog dialog = build.create();
    dialog.show();
    }
    });
    }
    //列表对话框的实现
    private void showDialog4() {
    bt4.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
    @Override
    public void onClick(View v) {
    build.setIcon(R.drawable.she);
    build.setTitle("你喜欢的人是:");
    build.setItems(listitem, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this, "我更喜欢:" +listitem[which], Toast.LENGTH_SHORT).show();
    }
    });
    AlertDialog dialog = build.create();
    dialog.show();
    }
    });
    }
    //自定义对话框的实现
    private void showDialog5() {
    bt5.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
    @Override
    public void onClick(View v) {
    //设置对话框标题
    build.settitle("测试你们的缘分");
    //设置对话框图标
                    build.setIcon(R.drawable.she);
    //创建视图
    LayoutInflater inflate=LayoutInflater.from(MainActivity.this);
    final View view=inflate.inflate(R.layout.view,null);
    // 加载视图
    build.setView(view);
    testbt= (Button) view.findViewById(R.id.testbt);
    //创建对话框
    AlertDialog dialog = build.create();
    testbt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)
    //想获取对话框中的EditText的值,必须在初始化的时候加所在View对象view 的findViewById方法
    edit=(EditText) view.findViewById(R.id.edit);
                            text1=(TextView)view.findViewById(R.id.text1);
    text1.setText("你与"+edit.getText()+"有三生三世十里桃花的缘分");
    }
    });
    build.setPositiveButton("退出", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this,"退出对话框",Toast.LENGTH_SHORT).show();
    //退出对话框
    dialog.dismiss();
    }
    });
    //展示对话框
    dialog.show();
    }
    });
    }
    }
    附view.xml配置文件
    <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="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="请输入你喜欢的人"/>
    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit"/>
    <Button
    android:text="测试缘分"
    android:id="@+id/testbt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout>
    <ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/she"/>
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:hint="即将揭晓你和她的缘分"/>
    </LinearLayout>
     
  • 相关阅读:
    Q:简单实现URL只能页面跳转,禁止直接访问
    Q:elementUI中tree组件动态展开
    一个切图仔的 JS 笔记
    一个切图仔的HTML笔记
    一个切图仔的 CSS 笔记
    GnuPG使用笔记
    SQL Svr 2012 Enterprise/Always-on节点连接超时导致节点重启——case分享
    网卡配置文件备份在原目录下引起网络配置异常
    python培训
    service脚本的写法
  • 原文地址:https://www.cnblogs.com/cct1314520/p/6443127.html
Copyright © 2011-2022 走看看