MainActivity.java
package com.chuanxidemo.shaoxin.demo03; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends BaseActivity { private RelativeLayout activityMain; private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button btn6; private AlertDialog.Builder builder; private int numId; private List data; private int num; private ProgressDialog progressDialog; @Override protected void setContentView() { setContentView(R.layout.activity_main); } @Override protected void setListener() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); } @Override protected void init() { btn1 = (Button) findViewById(R.id.btn_1); btn2 = (Button) findViewById(R.id.btn_2); btn3 = (Button) findViewById(R.id.btn_3); btn4 = (Button) findViewById(R.id.btn_4); btn5 = (Button) findViewById(R.id.btn_5); btn6 = (Button) findViewById(R.id.btn_6); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_1: builder = new AlertDialog.Builder(this); builder.setTitle("简单对话框"); builder.setMessage("我是简单对话框"); builder.create().show(); break; case R.id.btn_2: builder = new AlertDialog.Builder(this); builder.setTitle("普通对话框"); builder.setMessage("我是普通对话框"); builder.setPositiveButton("确认", null); builder.setNegativeButton("取消", null); builder.setNeutralButton("查看", null); builder.setCancelable(false); builder.create().show(); break; case R.id.btn_3: builder = new AlertDialog.Builder(this); builder.setTitle("单选对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(R.array.citysName, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { numId = which; } }); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.v("msg", getResources().getStringArray(R.array.citysName)[numId]); } }); builder.setCancelable(false); builder.create().show(); break; case R.id.btn_4: data = new ArrayList(); builder = new AlertDialog.Builder(this); builder.setTitle("多选对话框"); builder.setIcon(R.mipmap.ic_launcher); data.clear(); builder.setMultiChoiceItems(R.array.citysName, new boolean[]{false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String[] citys = getResources().getStringArray(R.array.citysName); if (isChecked) { data.add(citys[which]); } else { data.remove(citys[which]); } } }); builder.setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.v("msg", data.toString()); } }); builder.create().show(); break; case R.id.btn_5: final Dialog dialog = new Dialog(this); dialog.setTitle("自定义对话框"); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.diy_dialog, null); dialog.setContentView(view); Button login = (Button) view.findViewById(R.id.login); dialog.show(); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); break; case R.id.btn_6: progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setTitle("进度条对话框"); progressDialog.setMax(100); progressDialog.create(); progressDialog.show(); new MyThread(progressDialog).start(); break; } } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); progressDialog.setProgress(msg.arg1); } }; public class MyThread extends Thread { ProgressDialog progressDialog; public MyThread(ProgressDialog progressDialog) { this.progressDialog = progressDialog; } @Override public void run() { while (true) { try { Thread.sleep(100); Message message = new Message(); handler.sendEmptyMessage(0); if (num <= progressDialog.getMax()) { num = progressDialog.getProgress(); num++; message.arg1 = num; handler.sendMessage(message); }else { progressDialog.dismiss(); break; } } catch (InterruptedException e) { e.printStackTrace(); } } } } }
BaseActivity.java
package com.chuanxidemo.shaoxin.demo02; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; /** * Created by shaoxin on 2017/2/21. */ public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(); init(); setListener(); } protected abstract void setContentView(); protected abstract void setListener(); protected abstract void init(); }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.chuanxidemo.shaoxin.demo02.MainActivity"> <ImageView android:id="@+id/img" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <SeekBar android:id="@+id/seekbar" android:layout_below="@+id/img" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:layout_below="@+id/seekbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00.00"/> <Button android:id="@+id/btn" android:layout_centerHorizontal="true" android:layout_below="@id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放"/> </RelativeLayout>