網站首頁 編程語言 正文
本文實例為大家分享了Android自定義對話框的具體實現代碼,供大家參考,具體內容如下
1、定義對話框的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <TextView ? ? ? ? android:id="@+id/title" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:gravity="center" ? ? ? ? android:textSize="16sp" ? ? ? ? android:layout_margin="4dp" ? ? ? ? android:text="標題"/> ? ? <TextView ? ? ? ? android:id="@+id/content1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="14sp" ? ? ? ? android:text="第一行文字" ? ? ? ? android:layout_margin="4dp" ? ? ? ? android:layout_below="@id/title" ? ? ? ? android:gravity="center"/> ? ? <TextView ? ? ? ? android:id="@+id/content2" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="14sp" ? ? ? ? android:text="第一行文字" ? ? ? ? android:layout_margin="4dp" ? ? ? ? android:layout_below="@id/content1" ? ? ? ? android:gravity="center"/> ? ? <LinearLayout ? ? ? ? android:id="@+id/linear" ? ? ? ? android:layout_below="@id/content2" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal" ? ? ? ? android:layout_marginTop="6dp" ? ? ? ? android:paddingRight="20dp" ? ? ? ? android:paddingLeft="20dp" ? ? ? ? > ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/ok" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="14sp" ? ? ? ? ? ? android:text="確定"/> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/cancel" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:textSize="14sp" ? ? ? ? ? ? android:text="取消"/> ? ? </LinearLayout> ? ? <Button ? ? ? ? android:id="@+id/know" ? ? ? ? android:layout_below="@id/linear" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_marginTop="10dp" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="14sp" ? ? ? ? android:text="知道了"/> </RelativeLayout>
對話框樣式(比較丑哈,就是大概這個意思,嘿嘿)
2、定義接口
利用接口回調的方式使對話框消失。
public interface DialogListener {
? ? void onClick(MyDialog dialog,View view);
}
3、寫一個類繼承Dialog,并重寫構造方法
說明:第三個按鈕的監聽與其他兩個不同,前兩個使用的是button原聲的監聽事件,第三個為自定義的接口,目的是獲取MyDialog,然后通過dismiss()方法使對話框不顯示。(接口回調的方式)
public class MyDialog extends Dialog {
? ? private TextView mTipOneView;
? ? private TextView mTipTwoView;
? ? private TextView mTitleView;
? ? private Button mOkView;
? ? private Button mCancelView;
? ? private Button mKonwView;
? ? private View.OnClickListener mOkListener;
? ? private View.OnClickListener mCancelListener;
? ? private DialogListener mKnowListener;
? ? private String title;
? ? private String oneTip;
? ? private String twoTip;
? ? private void setOnDialogListener(DialogListener listener){
? ? ? ? this.mKnowListener = listener;
? ? }
? ? public MyDialog(Context context) {
? ? ? ? super(context);
? ? }
? ? public MyDialog(Context context,String title,String oneTip,String twoTip,View.OnClickListener ok,View.OnClickListener cancel,DialogListener know) {
? ? ? ? this(context);
? ? ? ? this.title = title;
? ? ? ? this.oneTip = oneTip;
? ? ? ? this.twoTip = twoTip;
? ? ? ? mOkListener = ok;
? ? ? ? mCancelListener = cancel;
? ? ? ? mKnowListener = know;
? ? }
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.layout_dialog);
? ? ? ? mCancelView = (Button) findViewById(R.id.cancel);
? ? ? ? mOkView = (Button) findViewById(R.id.ok);
? ? ? ? mKonwView = (Button) findViewById(R.id.know);
? ? ? ? mTipOneView = (TextView) findViewById(R.id.content1);
? ? ? ? mTipTwoView = (TextView) findViewById(R.id.content2);
? ? ? ? mTitleView = (TextView) findViewById(R.id.title);
? ? ? ? mTitleView.setText(title);
? ? ? ? mTipTwoView.setText(twoTip);
? ? ? ? mTipOneView.setText(oneTip);
? ? ? ? mCancelView.setOnClickListener(mCancelListener);
? ? ? ? mOkView.setOnClickListener(mOkListener);
? ? ? ? mKonwView.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? mKnowListener.onClick(MyDialog.this,view);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
通過setViewContent(R.layout.~)為對話框設置樣式;使用構造方法傳值。
4、顯示對話框
public class CustomDialogActivity extends AppCompatActivity {
? ? private DialogListener listener;
? ? private MyDialog myDialog;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_custom_dialog);
? ? ? ? listener = new DialogListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(MyDialog dialog, View view) {
? ? ? ? ? ? ? ? myDialog.dismiss();
? ? ? ? ? ? }
? ? ? ? };
? ? }
? ? public void showDialog(View view){
? ? ? ? ?myDialog = new MyDialog(CustomDialogActivity.this, "不知道", "有問題么", "啥問題", new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("----->", "ok");
? ? ? ? ? ? ? ? //點擊按鈕發生的事件
? ? ? ? ? ? }
? ? ? ? }, new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("----->", "cancle");
? ? ? ? ? ? ? ? //點擊按鈕發生的事件
? ? ? ? ? ? }
? ? ? ? },listener);
? ? ? ? myDialog.show();
? ? }
}
注意:一定不要忘了show(),否則對話框不顯示。
原文鏈接:https://blog.csdn.net/hello_1s/article/details/51683325
相關推薦
- 2022-11-24 Flask中Cookie和Session理解與作用介紹_python
- 2022-11-17 Python操作MongoDB的教程詳解(插,查,改,排,刪)_python
- 2023-03-04 Golang錯誤處理方式異常與error_Golang
- 2022-09-24 關于R語言包的升級與降級問題_R語言
- 2022-05-05 碎片拼接技術恢復XenServer服務器SQL?Server數據庫數據_XenServer
- 2022-06-14 Golang監聽日志文件并發送到kafka中_Golang
- 2023-10-13 Error: cannot push because a reference that you ar
- 2023-01-12 一文帶你入木三分地理解字符串KMP算法以及C++實現_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支