发布于 2017-03-23 13:14:21 | 120 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android对话框简单用法,涉及Android对话框的功能、定义、创建及使用等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android开发入门之对话框简单用法。分享给大家供大家参考,具体如下:

注:本文只是一个学习笔记 用以记录自己学到哪了

1.获得AlertDialog的静态内部类Builder对象,由此类来创建对话框
2.通过Builder对象设置对话框的标题 按钮以及按钮响应的事件
3.调用Builder的Create()方法创建对话框
4.调用AlertDialog的show()方法显示对话框

main.xml文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:id="@+id/MyTextView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<Button
  android:id="@+id/myButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="创建Alert对话框"
  />
</LinearLayout>

MainActivity文件


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myTextView = (TextView)findViewById(R.id.MyTextView);
    myButton = (Button)findViewById(R.id.myButton);
    //添加AlertDialog.Builder对象
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //为activity中按钮添加按钮事件
    myButton.setOnClickListener(new View.OnClickListener()
    {
    @Override
    public void onClick(View v)
    {
      builder.setTitle("您确定要删除此条信息?").
      //设置确定按钮
      setPositiveButton("Yes", new OnClickListener()
      {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
          myTextView.setText("删除成功");
        }
      }).
      //设置取消按钮
      setNegativeButton("No", new OnClickListener()
      {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
          myTextView.setText("取消删除");
        }
      });
       //创建对话框
        AlertDialog alertDialog = builder.create();
        //显示对话框
        alertDialog.show();
    }
    });
  }
}

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务