Android自定義對(duì)話框的簡(jiǎn)單實(shí)現(xiàn)
本文實(shí)例為大家分享了Android自定義對(duì)話框的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
1、定義對(duì)話框的布局
<?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="標(biāo)題"/> ? ? <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>
對(duì)話框樣式(比較丑哈,就是大概這個(gè)意思,嘿嘿)

2、定義接口
利用接口回調(diào)的方式使對(duì)話框消失。
public interface DialogListener {
? ? void onClick(MyDialog dialog,View view);
}3、寫(xiě)一個(gè)類繼承Dialog,并重寫(xiě)構(gòu)造方法
說(shuō)明:第三個(gè)按鈕的監(jiān)聽(tīng)與其他兩個(gè)不同,前兩個(gè)使用的是button原聲的監(jiān)聽(tīng)事件,第三個(gè)為自定義的接口,目的是獲取MyDialog,然后通過(guò)dismiss()方法使對(duì)話框不顯示。(接口回調(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);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}通過(guò)setViewContent(R.layout.~)為對(duì)話框設(shè)置樣式;使用構(gòu)造方法傳值。
4、顯示對(duì)話框
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, "不知道", "有問(wèn)題么", "啥問(wèn)題", new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("----->", "ok");
? ? ? ? ? ? ? ? //點(diǎn)擊按鈕發(fā)生的事件
? ? ? ? ? ? }
? ? ? ? }, new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? Log.e("----->", "cancle");
? ? ? ? ? ? ? ? //點(diǎn)擊按鈕發(fā)生的事件
? ? ? ? ? ? }
? ? ? ? },listener);
? ? ? ? myDialog.show();
? ? }
}注意:一定不要忘了show(),否則對(duì)話框不顯示。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android入門之彈出式對(duì)話框的實(shí)現(xiàn)
- Android入門之利用Spinner實(shí)現(xiàn)彈出選擇對(duì)話框
- Android對(duì)話框使用方法詳解
- 五分了解Android?Progress?Bar進(jìn)度條加載
- Android開(kāi)發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例
- Android自定義View實(shí)現(xiàn)進(jìn)度條動(dòng)畫(huà)
- Android實(shí)現(xiàn)簡(jiǎn)單實(shí)用的垂直進(jìn)度條
- android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果
- Jetpack Compose實(shí)現(xiàn)對(duì)話框和進(jìn)度條實(shí)例解析
相關(guān)文章
Android實(shí)現(xiàn)透明度可變的標(biāo)題欄效果
這篇文章主要介紹了Android實(shí)現(xiàn)透明度可變的標(biāo)題欄效果的相關(guān)資料,具有一定的參考價(jià)值,需要的朋友可以參考下2016-02-02
Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
今天小編就為大家分享一篇關(guān)于Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android開(kāi)發(fā)圖片水平旋轉(zhuǎn)180度方法
今天小編就為大家分享一篇Android開(kāi)發(fā)圖片水平旋轉(zhuǎn)180度方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
[Alibaba-ARouter]淺談簡(jiǎn)單好用的Android頁(yè)面路由框架
這篇文章主要介紹了[Alibaba-ARouter]淺談簡(jiǎn)單好用的Android頁(yè)面路由框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android 游戲引擎libgdx 資源加載進(jìn)度百分比顯示案例分析
因?yàn)榘咐容^簡(jiǎn)單,所以簡(jiǎn)單用AndroidApplication -> Game -> Stage 搭建框架感興趣的朋友可以參考下2013-01-01
Android自定義ViewGroup實(shí)現(xiàn)九宮格布局
這篇文章主要為大家詳細(xì)介紹了Android如何通過(guò)自定義ViewGroup實(shí)現(xiàn)九宮格布局,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
Android開(kāi)發(fā)島嶼數(shù)量算法示例解析
這篇文章主要為大家介紹了Android開(kāi)發(fā)島嶼數(shù)量算法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

