Android对话框
在一个例子中展示四种对话框。
设置四个按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt_dialog1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通对话框" />
<Button
android:id="@+id/bt_dialog2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" />
<Button
android:id="@+id/bt_dialog3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" />
<Button
android:id="@+id/bt_dialog4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进度条对话框" />
</LinearLayout>
分别是普通对话框、单选对话框、多选对话框、进度条对话框
package com.example.stylethemetest;
import android.app.ProgressDialog;
import android.content.Context;
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.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button btDialog1 = (Button) findViewById(R.id.bt_dialog1);
Button btDialog2 = (Button) findViewById(R.id.bt_dialog2);
Button btDialog3 = (Button) findViewById(R.id.bt_dialog3);
Button btDialog4 = (Button) findViewById(R.id.bt_dialog4);
btDialog1.setOnClickListener(this);
btDialog2.setOnClickListener(this);
btDialog3.setOnClickListener(this);
btDialog4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 1. 普通对话框
case R.id.bt_dialog1:
AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(mContext);
alertDialog1.setTitle("注意");
alertDialog1.setMessage("真的要删除吗?");
alertDialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了确定", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了取消", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.show();
break;
// 2. 单选对话框
case R.id.bt_dialog2:
final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(mContext);
alertDialog2.setTitle("选择学历");
final String[] edu = {"小学", "初中", "高中", "本科", "研究生", "博士", "其他"};
// 选择默认选中,-1表示不选中
alertDialog2.setSingleChoiceItems(edu, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String eduLevel = edu[which];
Toast.makeText(mContext, eduLevel+which, Toast.LENGTH_SHORT).show();
}
});
alertDialog2.setPositiveButton("选择", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO:处理确定逻辑
}
});
alertDialog2.show();
break;
// 3. 多选对话框
case R.id.bt_dialog3:
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(mContext);
alertDialog3.setTitle("选择你喜欢吃的水果");
final String[] items = {"榴莲", "苹果", "葡萄", "香蕉", "黄瓜", "火龙果", "荔枝"};
// bool值和上面的条目对应,true表示默认选中
final boolean[] checkedItems = {false, true, false, true, false,
false, false};
alertDialog3.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 下标和是否被选中
Toast.makeText(mContext, which+ " " +isChecked, Toast.LENGTH_SHORT).show();
}
});
alertDialog3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder fruits = new StringBuilder();
for (int i = 0; i < items.length; i++) {
if (checkedItems[i]) {
// 就证明是选中的
String fruit = items[i];
fruits.append(fruit).append(" ");
}
}
Toast.makeText(mContext, fruits, Toast.LENGTH_SHORT).show();
}
});
// show里面调用了create
alertDialog3.show();
break;
// 4. 进度条对话框
case R.id.bt_dialog4:
final ProgressDialog progressDialog = new ProgressDialog(mContext);
// 默认STYLE_SPINNER,小圆圈选装。可选水平进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("加载");
progressDialog.setMessage("Loading...");
// false表示用户可不能通过按下back键或者对话框外的地方退出,默认是true。
progressDialog.setCancelable(true);
progressDialog.show();
// 进入条相关组件可以在子线程更新UI
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 101; i++) {
// 与Thread.sleep不同的是,他不会抛出interruptedException;
SystemClock.sleep(50);
progressDialog.setProgress(i);
}
// cancel也调用了dismiss,不过还可可响应setOnCancelListener
// hide只是隐藏,没有dismiss掉
progressDialog.dismiss();
}
}).start();
break;
}
}
}
上图是普通对话框
上图是单选对话框
上图是多选对话框
上图是进度条对话框
Android帧动画初步认识
其实就是加载一系列的图片资源
- 首先放入一系列图片放入res/drawable中,我放入了drawable-xxhdpi。
-
在上述文件夹下新建一个xml,如下。
android:oneshot="false"
表示循环一次后还接着循环。true的话就是循环一次停止在最后一帧。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/a" android:duration="500" />
<item android:drawable="@drawable/b" android:duration="500" />
<item android:drawable="@drawable/c" android:duration="500" />
<item android:drawable="@drawable/d" android:duration="500" />
<item android:drawable="@drawable/e" android:duration="500" />
</animation-list>
主界面就放一个ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.animationtest.MainActivity">
<ImageView
android:id="@+id/iv_animation"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
MainActivity
package com.example.animationtest;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView rocketImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rocketImage = (ImageView) findViewById(R.id.iv_animation);
// [1]找到ImageView控件 用来显示动画效果
rocketImage = (ImageView) findViewById(R.id.iv_animation);
// [2]设置背景资源, 传入的是xml
rocketImage.setBackgroundResource(R.drawable.my_animation);
// [3]获取AnimationDrawable 类型
AnimationDrawable ad = (AnimationDrawable) rocketImage.getBackground();
// [4]开始执行动画
ad.start();
}
}
结果如下图片所示,其实是动态的。截图成静态了。
by @sunhaiyu
2017.5.18