Android studio自定義對(duì)話框效果
本文實(shí)例為大家分享了Android studio自定義對(duì)話框效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(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對(duì)象
第三步:調(diào)用inflater()方法獲取View對(duì)象
第四步:調(diào)用Builder對(duì)象的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="對(duì)話框" 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)建對(duì)話框 dialog.show();//顯示對(duì)話框 myview.findViewById(R.id.but).setOnClickListener( new View.OnClickListener() {//獲取布局里面按鈕 @Override public void onClick(View v) { dialog.dismiss();//點(diǎn)擊按鈕對(duì)話框消失 Toast.makeText( Main2Activity.this, "點(diǎn)擊了殘忍離開", Toast.LENGTH_SHORT ).show(); } } ); } }
點(diǎn)擊對(duì)話框
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)單選對(duì)話框
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android?Studio使用自定義對(duì)話框效果
相關(guān)文章
Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題
最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下2015-05-05A07_TimePicker & DatePicker & AnalogClock & Digi
本文將帶領(lǐng)大家一起學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈2013-06-06flutter TextField換行自適應(yīng)的實(shí)現(xiàn)
這篇文章主要介紹了flutter TextField換行自適應(yīng)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Android檢測(cè)IBeacon熱點(diǎn)的方法
這篇文章主要介紹了Android檢測(cè)IBeacon熱點(diǎn)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
在使用HttpURLConnection.getResponseCode()的時(shí)候直接報(bào)錯(cuò)是IOException錯(cuò)誤,一直想不明白,同一個(gè)程序我調(diào)用了兩次,結(jié)果有一個(gè)鏈接一直O(jiān)K,另一個(gè)卻一直報(bào)這個(gè)錯(cuò)誤2013-06-06Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問(wèn)
這篇文章主要介紹了Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)毛玻璃效果的對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)毛玻璃效果的對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02