1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| package com.gallifrey.todayapplication;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
import java.util.ArrayList;
public class DialogActivity extends AppCompatActivity { private Button mBtnNormal, mBtnList, mBtnSingle, mBtnMulti, mBtnEdit, mBtnCustom; private Button mBtnCircle,mBtnhori;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); mBtnNormal = findViewById(R.id.btn_normal); mBtnList = findViewById(R.id.btn_list); mBtnSingle = findViewById(R.id.btn_single); mBtnMulti = findViewById(R.id.btn_multi); mBtnEdit = findViewById(R.id.btn_edit); mBtnCustom = findViewById(R.id.btn_custom); mBtnCircle =findViewById(R.id.btn_circle_progress); mBtnhori=findViewById(R.id.btn_hori_progress);
MyDialogClickListener myDialogClickListener = new MyDialogClickListener(); mBtnCustom.setOnClickListener(myDialogClickListener); mBtnNormal.setOnClickListener(myDialogClickListener); mBtnSingle.setOnClickListener(myDialogClickListener); mBtnEdit.setOnClickListener(myDialogClickListener); mBtnMulti.setOnClickListener(myDialogClickListener); mBtnList.setOnClickListener(myDialogClickListener); mBtnhori.setOnClickListener(myDialogClickListener); mBtnCircle.setOnClickListener(myDialogClickListener); }
private class MyDialogClickListener implements View.OnClickListener {
@Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_normal: normalDialog(); break; case R.id.btn_list: listDialog(); break; case R.id.btn_single: singleDialog(); break; case R.id.btn_multi: multiDialog(); break; case R.id.btn_edit: editDialog(); break; case R.id.btn_custom: customDialog(); break; case R.id.btn_hori_progress: horiProgressDialog(); break; case R.id.btn_circle_progress: circleProgressDialog(); } } }
private void circleProgressDialog() { ProgressDialog circleD=new ProgressDialog(this); circleD.setIcon(R.mipmap.icon_dialog); circleD.setTitle("环状滚动条"); circleD.setMessage("努力加载中......."); circleD.setProgressStyle(ProgressDialog.STYLE_SPINNER); circleD.show(); }
private void horiProgressDialog() { ProgressDialog horiProgress=new ProgressDialog(this); horiProgress.setIcon(R.mipmap.icon_dialog); horiProgress.setTitle("水平滚动条"); horiProgress.setMessage("努力加载中......"); horiProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); horiProgress.setProgress(0); horiProgress.setMax(100); horiProgress.setSecondaryProgress(3); horiProgress.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this,"点击确定",Toast.LENGTH_LONG); dialogInterface.cancel(); } }); horiProgress.show(); new Thread(new Runnable() { @Override public void run() { while(horiProgress.getProgress()<horiProgress.getMax()){ try { Thread.sleep(100); horiProgress.incrementProgressBy(1); horiProgress.incrementSecondaryProgressBy(2); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }
private void customDialog() { View view= LayoutInflater.from(this).inflate(R.layout.layout_custom,null,false); AlertDialog.Builder customD=new AlertDialog.Builder(this); EditText etUser=view.findViewById(R.id.et_user); Button mBtnCustomOk=view.findViewById(R.id.btn_custom_ok); customD.setView(view).show(); mBtnCustomOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(DialogActivity.this,etUser.getText(),Toast.LENGTH_LONG).show(); } }); }
private void editDialog() { AlertDialog.Builder editD=new AlertDialog.Builder(this); EditText editText=new EditText(this); editD.setIcon(R.mipmap.icon_dialog) .setTitle("输入对话框") .setView(editText) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this,"内容: "+editText.getText(),Toast.LENGTH_LONG).show(); } }) .show(); }
ArrayList<Integer> choices =new ArrayList<>(); private void multiDialog() {
String[] items={"绘画","唱歌","足球","吉他"}; boolean[] checkedItems={true,false,false,true}; choices.clear(); choices.add(0); choices.add(3); AlertDialog.Builder multiD=new AlertDialog.Builder(this); multiD.setIcon(R.mipmap.icon_dialog) .setTitle("多选对话框") .setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { if(b){ choices.add(i); }else{ choices.remove(Integer.valueOf(i)); } } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { StringBuilder str=new StringBuilder(); for (int j = 0; j < choices.size(); j++) { str.append(items[choices.get(j)]+" "); } Toast.makeText(DialogActivity.this,"兴趣爱好:"+str,Toast.LENGTH_LONG).show(); } }).show(); }
private void singleDialog() { String[] items ={"优秀","良好","合格","不合格"}; AlertDialog.Builder singleD=new AlertDialog.Builder(this); final int[] choice = new int[1]; singleD.setIcon(R.mipmap.icon_dialog) .setTitle("单选对话框") .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { choice[0] =i;
} }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this,"单选结果: "+items[choice[0]],Toast.LENGTH_LONG).show(); } }).show(); }
private void listDialog() { String[] items={"梅花","杏花","桃花","荷花","蔷薇","烟花"}; AlertDialog.Builder listD=new AlertDialog.Builder(this); listD.setIcon(R.mipmap.icon_dialog2) .setTitle("列表对话框") .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this,"花: "+items[i],Toast.LENGTH_LONG).show(); } }).show();
}
private void normalDialog() { AlertDialog.Builder normalD = new AlertDialog.Builder(this); normalD.setIcon(R.mipmap.icon_dialog) .setTitle("普通对话框") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this, "Ok", Toast.LENGTH_LONG).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this, "OK", Toast.LENGTH_LONG).show(); } }).setNeutralButton("再看看", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(DialogActivity.this, "OK", Toast.LENGTH_LONG).show(); } }).show(); } }
|