从零开始学android -- dialog
先看个效果图
activity_main.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"> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="复选对话框"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载对话框"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示进度的加载对话框"
/> <Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="推荐创建对话框的方式创建对话框"/>
</LinearLayout>
DialogActivity.class
package zou.study.com.myfirstapp; import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import zou.study.com.fragment.EditNameDialogFragment; public class DialogActivity extends AppCompatActivity implements View.OnClickListener{ String[] items = {"Google","Apple","Microsoft"};
boolean[] itemChecked = new boolean[items.length]; ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
} @Override
public void onClick(View v) { switch (v.getId()){
case R.id.button1: //普通的复选对话框
showDialog(1);
break;
case R.id.button2: //进度对话框
final ProgressDialog dialog = ProgressDialog.show(this,"Do something","Please wait...");
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(5000);
dialog.dismiss();
}
}).start();
break;
case R.id.button3: //带进度调的对话框
showDialog(2);
progressDialog.setProgress(0);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0;i <= 15;i++){
SystemClock.sleep(1000);
progressDialog.incrementProgressBy(100/15);
}
progressDialog.dismiss();
}
}).start();
break;
case R.id.button4: //采用android 3.0后推荐的创建dialog方式 DialogFragment 这里只是简单用法,想要了解更多请自己google
EditNameDialogFragment editNameDialog = new EditNameDialogFragment();
editNameDialog.show(getFragmentManager(), "EditNameDialog");
break;
}
} @Override
protected Dialog onCreateDialog(int id) {
switch (id){
case 1:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("标题");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"OK clicked",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"cancel clicked",Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, itemChecked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getBaseContext(),items[which] + (isChecked?"checked!":"unchecked!"),Toast.LENGTH_SHORT).show();
}
});
return builder.create();
case 2:
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setTitle("Downloading files...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"OK clicked",Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(),"cancel clicked",Toast.LENGTH_SHORT).show();
}
});
return progressDialog;
}
return null;
}
}
EditNameDialogFragment
package zou.study.com.fragment; import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import zou.study.com.myfirstapp.R; public class EditNameDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉action return inflater.inflate(R.layout.fragment_dialog,container);
}
}
fragment_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/id_label_your_name"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:gravity="center_vertical"
android:text="Your name:" /> <EditText
android:id="@+id/id_txt_your_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/id_label_your_name"
android:imeOptions="actionDone"
android:inputType="text" /> <Button
android:id="@+id/id_sure_edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/id_txt_your_name"
android:text="ok" /> </RelativeLayout>
学习记录之用,如有错误请指正谢谢.
从零开始学android -- dialog的更多相关文章
- 从零开始学android -- Service
废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...
- 从零开始学android开发-项目打包发布
右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...
- 从零开始学android开发-adt-bundle-eclipse下的修改android app名称
eclipse中,打开项目根目录中的AndoirManifest.xml文件,找到如下内容 <application android:allowBackup="true" a ...
- 从零开始学android开发-通过WebService进行网络编程,使用工具类轻松实现
相信大家在平常的开发中,对网络的操作用到HTTP协议比较多,通过我们使用Get或者Post的方法调用一个数据接口,然后服务器给我们返回JSON格式的数据,我们解析JSON数据然后展现给用户,相信很多人 ...
- 从零开始学android开发-通过WebService获取今日天气情况
因为本身是在搞.NET方面的东东,现在在学习Android,所以想实现Android通过WebService接口来获取数据,网上很多例子还有有问题的.参考:Android 通过WebService进行 ...
- 从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight
线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...
- 从零开始学android开发-setBackgroundDrawable与setBackgroundResource的区别
setBackgroundDrawable和setBackgroundResource的区别很多网友不知道View类提供的setBackgroundDrawable和setBackgroundReso ...
- 从零开始学android开发-用Intent启动Activity的方法
启动另外一个Activity,可以有的方法有用setClass()和Component Name 1. 先说在setClass启动一个Activity的方法吧: Intent intent = new ...
- 从零开始学android开发-View的setOnClickListener的添加方法
1)第一种,也是最长见的添加方法(一下都以Button为例) Button btn = (Button) findViewById(R.id.myButton); btn .setOnClickLis ...
随机推荐
- 我学MSMQ(一)
一.通过这篇文章主要是对自己学习MSMQ进行小结,并希望能把自己的想法写出来,能和一些也正在研究MSMQ的朋友共同学习,并希望能给予指导和建议 二.首先是MSMQ的一些理论上的知识 ...
- 【Linux】linux命令大全
[注意]:命令[compgen -b]可以列出所有当前系统支持的命令. 109个Linux命令 目录 1 文件管理... 5 1.1 basename. 5 1.2 ...
- Ubuntu16.04 -- 后台进程Nohup
nohup用于使程序在用户退出登陆.关闭终端之后仍能继续运行 用法: nohup your_command & #(符号&使程序在后台运行) exit #(退出nohup模式) 启动后 ...
- apache 的rewrite函数配置伪静态
配置伪静态目的:对于访问比较长的uri,利于网站搜索工具更容易记住,换句话利于SEO 在配置文件中添加或找到 <IfModule mod_rewrite.c> </IfModule& ...
- django 配置上传图片和文件
在django中经常遇到要上传文件的需求,这里记录下如何配置用户上传的文件保存 首先在setting中添加 TEMPLATES = [ { 'BACKEND': 'django.template.ba ...
- django验证码django-simple-captha
搭建网站很经常要用到验证码,django中就有这样的中间件django-simple-captha githup地址https://github.com/mbi/django-simple-captc ...
- ElasticSearch 排序
1.相关性排序 ElasticSearch为了按照相关性来排序,需要将相关性表示为一个数值,在 Elasticsearch 中, 相关性得分 由一个浮点数进行表示,并在搜索结果中通过 _score 参 ...
- python 小技巧(glob,guid,序列化,压缩字符,有序字典,sorted函数,分片)
1.glob模块 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:”*”, “?”, “[]”.”* ...
- Redis源代码分析(十三)--- redis-benchmark性能測试
今天讲的这个是用来给redis数据库做性能測试的,说到性能測试,感觉这必定是高大上的操作了.redis性能測试.測的究竟是哪方面的性能,怎样測试,通过什么指标反映此次測试的性能好坏呢.以下我通过源代码 ...
- Java三大器之监听器(Listener)的工作原理和代码演示
现在来说说Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次, ...