android 之Dialog的多种Dialog对话框(以及反射在自定义对话框的运用)
生活随笔
收集整理的這篇文章主要介紹了
android 之Dialog的多种Dialog对话框(以及反射在自定义对话框的运用)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)際效果:
實(shí)例代碼:
一個(gè)"啟動(dòng)初始Activity.xml"
一個(gè)彈出的Activity
業(yè)務(wù)邏輯代碼:
package com.hsj.example.alertdialogdemo04;import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast;import java.lang.reflect.Field;public class MainActivity extends AppCompatActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}private AlertDialog dialog;/*** 自定義對(duì)話框* @param view*/public void customerDialog(View view){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("登錄提示");builder.setCancelable(false);View loginView=View.inflate(this,R.layout.login_view_02,null);final EditText editText_userName= (EditText) loginView.findViewById(R.id.editText_userName);final EditText editText_pwd= (EditText) loginView.findViewById(R.id.editText_pwd);builder.setView(loginView);//設(shè)置對(duì)話框的自定義View布局builder.setPositiveButton("登錄", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String userName=editText_userName.getText().toString();String pwd=editText_pwd.getText().toString();if(TextUtils.isEmpty(userName)){editText_userName.setError("用戶名必須輸入");editText_userName.requestFocus();closeDialog(dialog,false);return;}editText_pwd.requestFocus();if(TextUtils.isEmpty(pwd)){editText_pwd.setError("密碼必須輸入!");editText_pwd.requestFocus();closeDialog(dialog,false);return;}if("admin".equals(userName) && "123".equals(pwd)){Toast.makeText(getApplicationContext(),userName+"登錄成功",Toast.LENGTH_LONG).show();closeDialog(dialog,true);}else {closeDialog(dialog,false);Toast.makeText(getApplicationContext(),"用戶名或者密碼錯(cuò)誤!",Toast.LENGTH_LONG).show();}}});builder.setNegativeButton("重置", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {editText_userName.setText(null);editText_pwd.setText("");editText_userName.requestFocus();}});dialog= builder.show();}/*** 決定是否關(guān)閉對(duì)話框* @param dialog 對(duì)話框?qū)ο? @param isClosed 對(duì)話框是否顯示 true:關(guān)閉 false:不關(guān)閉*/private void closeDialog(DialogInterface dialog, boolean isClosed) {//1.得到當(dāng)前AlertDialog 的類模板對(duì)象Class clz_alertDialog= dialog.getClass();//2.得到當(dāng)前類的父類的類模板Class clz_dialog=clz_alertDialog.getSuperclass();try {Field mShowing_Filed= clz_dialog.getDeclaredField("mShowing");//如果私有字段想在類外訪問(wèn),則必須取消java 語(yǔ)法檢查mShowing_Filed.setAccessible(true);mShowing_Filed.set(dialog,isClosed);} catch (Exception e) {e.printStackTrace();}System.out.println("==closeDialog(DialogInterface dialog="+dialog+", boolean isClosed="+isClosed+")==");} }1.列表對(duì)話框的使用
2.單選列表對(duì)話框的使用
3.復(fù)選列表對(duì)話框的使用
123示例代碼:
public class MainActivity_bak03 extends AppCompatActivity implements DialogInterface.OnClickListener{private CharSequence[] fruits =new CharSequence[]{"香蕉","蘋果","哈密瓜","西瓜","西紅柿"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/*** 列表對(duì)話框的使用* @param view*/public void list(View view){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("水果提示");//builder.setMessage("看看有你喜歡的水果嗎?");如果設(shè)置其他類型的對(duì)話框則這行代碼必須不寫,否則無(wú)效果builder.setItems(fruits,this);//設(shè)置當(dāng)前對(duì)話框?yàn)榱斜韺?duì)話框builder.create().show();}/*** 當(dāng)用戶點(diǎn)擊對(duì)應(yīng)的條目時(shí)自動(dòng)調(diào)用的方法* @param dialog 對(duì)話框* @param which 用戶點(diǎn)擊條目對(duì)應(yīng)的索引值*/@Overridepublic void onClick(DialogInterface dialog, int which) {CharSequence fruit=fruits[which];Toast.makeText(this, "fruit="+fruit, Toast.LENGTH_SHORT).show();System.out.println("===onClick(DialogInterface dialog="+dialog+", int which="+which+") ====");//銷毀對(duì)話框dialog.dismiss();}/*** 單選列表對(duì)話框的使用* @param view*/public void radioList(View view){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("水果提示");builder.setSingleChoiceItems(fruits,//單選列表對(duì)話框的數(shù)據(jù)源2,//哪些數(shù)據(jù)被選中,-1:表示不選中.0代表索引為0的數(shù)據(jù)項(xiàng)被選中,1:代表索引為1的數(shù)據(jù)項(xiàng)被選中,依次類推....this);//設(shè)置單選列表對(duì)話框builder.create().show();}/*** 復(fù)選列表對(duì)話框的使用* @param view*/public void multiList(View view){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("水果提示");//指定數(shù)據(jù)源中對(duì)應(yīng)的數(shù)據(jù)項(xiàng)是否被選中,true 被選中,false 未被選中;這個(gè)數(shù)組中元素的個(gè)數(shù)一定要和數(shù)據(jù)源中數(shù)據(jù)元素的個(gè)數(shù)一一對(duì)應(yīng),//否則,如果當(dāng)前boolean 數(shù)組中元素的個(gè)數(shù)多于數(shù)據(jù)源中源的個(gè)數(shù),沒(méi)有問(wèn)題,但如果小于數(shù)據(jù)源中源的個(gè)數(shù)則報(bào)索引越界異常:java.lang.ArrayIndexOutOfBoundsException: length=4; index=4final boolean[] checkedList =new boolean[]{true,false,true,false,true};builder.setMultiChoiceItems(fruits, checkedList, new DialogInterface.OnMultiChoiceClickListener() {/*** 當(dāng)復(fù)選框列表項(xiàng)狀態(tài)發(fā)生變化時(shí)自動(dòng)調(diào)用的方法* @param dialog 對(duì)話框?qū)ο? @param which 選中條目的索引值* @param isChecked true 當(dāng)前項(xiàng)被選中 false 當(dāng)前項(xiàng)取消選中*/@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {System.out.println("==onClick(DialogInterface dialog="+dialog+", int which="+which+", boolean isChecked="+isChecked+")===");System.out.println("checkedList="+ Arrays.toString(checkedList));/*StringBuilder sb=new StringBuilder();int len=fruits.length;for(int i=0;i<len;i++){if(checkedList[i]){sb.append(fruits[i]).append(",");}}if(sb.length()>0){//刪除指定索引位的字符sb.deleteCharAt(sb.length()-1);}Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_LONG).show();*/}});builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {StringBuilder sb=new StringBuilder();int len=fruits.length;for(int i=0;i<len;i++){if(checkedList[i]){sb.append(fruits[i]).append(",");}}if(sb.length()>0){//刪除指定索引位的字符sb.deleteCharAt(sb.length()-1);}Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_LONG).show();}});builder.show();}總結(jié)
以上是生活随笔為你收集整理的android 之Dialog的多种Dialog对话框(以及反射在自定义对话框的运用)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android 之Dialog对话框(简
- 下一篇: android 之Fragment(轻量