Android自定義對話框的簡單實現(xiàn)
本文實例為大家分享了Android自定義對話框的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
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、定義接口
利用接口回調(diào)的方式使對話框消失。
public interface DialogListener { ? ? void onClick(MyDialog dialog,View view); }
3、寫一個類繼承Dialog,并重寫構造方法
說明:第三個按鈕的監(jiān)聽與其他兩個不同,前兩個使用的是button原聲的監(jiān)聽事件,第三個為自定義的接口,目的是獲取MyDialog,然后通過dismiss()方法使對話框不顯示。(接口回調(diào)的方式)
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"); ? ? ? ? ? ? ? ? //點擊按鈕發(fā)生的事件 ? ? ? ? ? ? } ? ? ? ? }, new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? Log.e("----->", "cancle"); ? ? ? ? ? ? ? ? //點擊按鈕發(fā)生的事件 ? ? ? ? ? ? } ? ? ? ? },listener); ? ? ? ? myDialog.show(); ? ? } }
注意:一定不要忘了show(),否則對話框不顯示。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 使用ViewPager實現(xiàn)圖片左右循環(huán)滑動自動播放
這篇文章主要介紹了Android 使用ViewPager實現(xiàn)圖片左右循環(huán)滑動自動播放的相關資料,非常不錯,具有參考解決價值,需要的朋友可以參考下2016-08-08Android ViewPager實現(xiàn)輪播圖效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)輪播圖效果的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android 給圖片加上水印的示例代碼(支持logo+文字)
本篇文章主要介紹了Android 給圖片加上水印的示例代碼(支持logo+文字),具有一定的參考價值,有興趣的可以了解一下2017-08-08android 調(diào)用系統(tǒng)的照相機和圖庫實例詳解
android手機有自帶的照相機和圖庫,我們做的項目中有時用到上傳圖片到服務器,今天做了一個項目用到這個功能,所以把我的代碼記錄下來和大家分享,有需求的朋友可以參考下2012-12-12Android應用開發(fā)中CardView的初步使用指南
這篇文章主要介紹了Android應用開發(fā)中CardView的初步使用指南,CardView主要處理一些卡片型的視圖布局,需要的朋友可以參考下2016-02-02