android自定義AlertDialog對話框
前面一篇文章http://www.dbjr.com.cn/article/103036.htm介紹了alertDialog的四種簡單使用,但是有些時(shí)候?yàn)榱俗屨麄€(gè)app的風(fēng)格統(tǒng)一,或者說前面的四種形式不符合項(xiàng)目中的需求,這個(gè)時(shí)候就需要我們自定義alertDialog了。直接上代碼
CustomAlertDialog:
import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Created by yk on 2017/1/17. */ public class CustomAlertDialog extends Dialog implements View.OnClickListener { public interface OnDialogButtonClickListener { /** * 點(diǎn)擊按鈕的回調(diào)方法 * * @param requestCode * @param isPositive */ void onDialogButtonClick(int requestCode, boolean isPositive); } private Context context; private String title; private String message; private String strPositive; private String strNegative; private int requestCode; private OnDialogButtonClickListener listener; public CustomAlertDialog(Context context, String title, String message, int requestCode, OnDialogButtonClickListener listener) { super(context, R.style.MyDialog); this.context=context; this.title=title; this.message=message; this.requestCode=requestCode; this.listener=listener; } private TextView tvTitle; private TextView tvMessage; private Button btnPositive; private Button btnNegative; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alert_dialog); // setCancelable(false);//設(shè)置點(diǎn)擊對話框外部和按返回鍵都不可以取消 // setCanceledOnTouchOutside(false);//設(shè)置點(diǎn)擊對話框外部是否可以取消,默認(rèn)是不可以取消(但是點(diǎn)返回鍵可以取消) tvTitle = (TextView) findViewById(R.id.tvAlertDialogTitle); tvMessage = (TextView) findViewById(R.id.tvAlertDialogMessage); btnPositive = (Button) findViewById(R.id.btnAlertDialogPositive); btnNegative = (Button) findViewById(R.id.btnAlertDialogNegative); tvTitle.setText(title); tvMessage.setText(message); btnPositive.setOnClickListener(this); btnNegative.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnAlertDialogPositive: //確定按鈕 listener.onDialogButtonClick(requestCode,true); break; case R.id.btnAlertDialogNegative: //取消按鈕 listener.onDialogButtonClick(requestCode,false); break; } dismiss(); } }
MainActivity:
package com.example.yk.dialogtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class CustomDialogActivity extends AppCompatActivity implements CustomAlertDialog.OnDialogButtonClickListener { private static final int REQUEST_CODE_FIRST = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_dialog); CustomAlertDialog alertDialog=new CustomAlertDialog(this,"title","message",REQUEST_CODE_FIRST,this); alertDialog.show(); } @Override public void onDialogButtonClick(int requestCode, boolean isPositive) { if(requestCode==REQUEST_CODE_FIRST && isPositive){ Toast.makeText(this, "點(diǎn)擊了確定", Toast.LENGTH_SHORT).show(); } else if (requestCode==REQUEST_CODE_FIRST && !isPositive) { Toast.makeText(this, "點(diǎn)擊了取消", Toast.LENGTH_SHORT).show(); } } }
實(shí)現(xiàn)的效果:
如果我們需要自定義“確定”和“取消”按鈕所顯示的字,可以再寫一個(gè)構(gòu)造方法,比如:
public AlertDialog(Context context, String title, String message,String strPositive, String strNegative, int requestCode, OnDialogButtonClickListener listener) { super(context, R.style.MyDialog); this.context = context; this.title = title; this.message = message; this.strPositive = strPositive; this.strNegative = strNegative; this.requestCode = requestCode; this.listener = listener; }
if(!TextUtils.isEmpty(strPositive)){ btnPositive.setText(strPositive); } if(!TextUtils.isEmpty(strNegative)){ btnNegative.setText(strNegative); }
實(shí)現(xiàn)的效果:
源碼(包括上篇文章中四種簡單alertDialog的使用):http://xiazai.jb51.net/201701/yuanma/androidAlertDialog(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對話框的方法
- Android中AlertDialog各種對話框的用法實(shí)例詳解
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對話框功能
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對話框的功能
- Android修改源碼解決Alertdialog觸摸對話框邊緣消失的問題
- Android 自定義AlertDialog對話框樣式
- Android對話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對話框AlertDialog使用示例
- Android Alertdialog(實(shí)現(xiàn)警告對話框)
- Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對話框
相關(guān)文章
Android string-array數(shù)據(jù)源簡單使用
這篇文章主要介紹了Android string-array數(shù)據(jù)源簡單使用的相關(guān)資料,需要的朋友可以參考下2016-09-09Android AlertDialog實(shí)現(xiàn)分享對話框/退出對話框/下載對話框
這篇文章主要介紹了Android AlertDialog實(shí)現(xiàn)分享對話框/退出對話框/下載對話框的相關(guān)資料,需要的朋友可以參考下2016-04-04Android播放assets文件里視頻文件相關(guān)問題分析
這篇文章主要介紹了Android播放assets文件里視頻文件相關(guān)問題分析,結(jié)合Android播放assets文件出現(xiàn)錯(cuò)誤的實(shí)際問題給出了原因分析與解決方法參考,需要的朋友可以參考下2016-08-08Android自定義View實(shí)現(xiàn)圓形切圖效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形切圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12android自動安裝apk代碼實(shí)例(不使用apk安裝器安裝)
這篇文章主要介紹了android自動安裝apk代碼實(shí)例,代碼簡單,大家參考使用吧2013-11-11Android AutoWrapTextView中英文排版問題的解決方法
這篇文章主要給大家介紹了關(guān)于Android AutoWrapTextView中英文排版問題的解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05android中可以通過兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實(shí)現(xiàn)代碼,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02