Android studio自定義對話框效果
本文實例為大家分享了Android studio自定義對話框效果的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)步驟:
第一步:自定義.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="提示" android:background="@drawable/click" android:textSize="28sp"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center" android:text="你真的要離開嗎" android:textColor="#ff0400" android:background="@drawable/background" android:textSize="28sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="50sp" android:orientation="horizontal" android:background="@drawable/bb" android:gravity="center"> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="殘忍離開" android:textSize="24sp"/> </LinearLayout> </LinearLayout>
第二步:獲取layoutInflater對象
第三步:調(diào)用inflater()方法獲取View對象
第四步:調(diào)用Builder對象的setView()方法設(shè)置View
第五步:獲取輸入內(nèi)容或者監(jiān)聽事件等
默認(rèn)布局文件:.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="對話框" android:onClick="onclick"/> </LinearLayout>
.java:
package com.example.catalogin; import android.app.Dialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main2); } Dialog dialog; public void onclick(View v){ LayoutInflater inflater=LayoutInflater.from( this ); View myview=inflater.inflate(R.layout.catalogin,null);//引用自定義布局 AlertDialog.Builder builder=new AlertDialog.Builder( this ); builder.setView( myview ); dialog=builder.create();//創(chuàng)建對話框 dialog.show();//顯示對話框 myview.findViewById(R.id.but).setOnClickListener( new View.OnClickListener() {//獲取布局里面按鈕 @Override public void onClick(View v) { dialog.dismiss();//點擊按鈕對話框消失 Toast.makeText( Main2Activity.this, "點擊了殘忍離開", Toast.LENGTH_SHORT ).show(); } } ); } }
點擊對話框
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實現(xiàn)單選對話框
- Android中自定義對話框(Dialog)的實例代碼
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- Android自定義對話框Dialog的簡單實現(xiàn)
- 詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法
- 實例詳解Android自定義ProgressDialog進(jìn)度條對話框的實現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)
- Android中AlertDialog各種對話框的用法實例詳解
- Android?Studio使用自定義對話框效果
相關(guān)文章
A07_TimePicker & DatePicker & AnalogClock & Digi
本文將帶領(lǐng)大家一起學(xué)習(xí)時間日期和時鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈2013-06-06flutter TextField換行自適應(yīng)的實現(xiàn)
這篇文章主要介紹了flutter TextField換行自適應(yīng)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Android HttpURLConnection.getResponseCode()錯誤解決方法
在使用HttpURLConnection.getResponseCode()的時候直接報錯是IOException錯誤,一直想不明白,同一個程序我調(diào)用了兩次,結(jié)果有一個鏈接一直O(jiān)K,另一個卻一直報這個錯誤2013-06-06Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問
這篇文章主要介紹了Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02