zoukankan      html  css  js  c++  java
  • android_alertDialog

    主文件

    package cn.com.sxp;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class AlertDialogActivity extends Activity implements OnClickListener {
    private Button btnOne = null;
    private Button btnTwo = null;
    private Button btnThree = null;
    private AlertDialog.Builder builder = null;
    private AlertDialog alert = null;
    final CharSequence[] items = {"红", "绿", "蓝"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);
    btnThree = (Button) findViewById(R.id.btnThree);

    btnOne.setOnClickListener(this);
    btnTwo.setOnClickListener(this);
    btnThree.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnOne:
    // Builder构造函数
    // Constructor using a context for this builder and the AlertDialog it creates.
    builder = new AlertDialog.Builder(this);
    // setMessage:
    // Set the message to display.
    builder.setMessage("我乃石大虾")
    // setCancelable
    // Sets whether the dialog is cancelable or not. Default is true.
    .setCancelable(false)
    // setPositiveButton
    // Set a listener to be invoked when the positive button of the dialog is pressed.
    .setPositiveButton("是", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    AlertDialogActivity.this.finish();
    }
    })
    .setNegativeButton("不", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
    }
    });
    alert = builder.create();
    alert.show();
    break;
    case R.id.btnTwo:
    builder = new AlertDialog.Builder(this);
    builder.setTitle("请选择一种颜色");
    // setItems
    // Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.
    builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
    });
    alert = builder.create();
    alert.show();
    break;
    case R.id.btnThree:
    builder = new AlertDialog.Builder(this);
    builder.setTitle("请选择一种颜色,与上面不一样");
    // setSingleChoiceItems
    // Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list
    // will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog.
    // Clicking on a button will dismiss the dialog.
    // -1表示没有相被选择
    // builder.setMultiChoiceItems(items, -1, listener)
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
    });
    alert = builder.create();
    alert.show();
    default:

    }
    }
    }

    XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical" >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="@string/hello" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content"
    android:id
    ="@+id/btnOne"
    android:text
    ="一般" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content"
    android:id
    ="@+id/btnTwo"
    android:text
    ="列表" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content"
    android:id
    ="@+id/btnThree"
    android:text
    ="可选" />

    </LinearLayout>

    运行效果:

    点击一;

    点击二;

    点击三



     

  • 相关阅读:
    Java设计模式之单例模式
    sql查询优化整理
    MYSQL 调优学习笔记
    记一次失败的大厂面试
    ElasticSearch 6.3.2 整合 Springboot 2.1.10.RELEASE 版本,使用 Logstash 导入 mysql 数据
    ajax技术实现登录判断用户名是否重复以及利用xml实现二级下拉框联动
    浅谈 KMP 算法
    转载:Docker入门只需看这一篇就够了
    Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去
    hadoop入门之海量Web日志分析 用Hadoop提取KPI统计指标
  • 原文地址:https://www.cnblogs.com/itblog/p/2322897.html
Copyright © 2011-2022 走看看