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="你真的要離開(kāi)嗎"
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="殘忍離開(kāi)"
android:textSize="24sp"/>
</LinearLayout>
</LinearLayout>
第二步:獲取layoutInflater對(duì)象
第三步:調(diào)用inflater()方法獲取View對(duì)象
第四步:調(diào)用Builder對(duì)象的setView()方法設(shè)置View
第五步:獲取輸入內(nèi)容或者監(jiān)聽(tīng)事件等
默認(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)擊了殘忍離開(kāi)", 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)文章
詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android自定義相機(jī)界面的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)界面的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android數(shù)據(jù)持久化之讀寫(xiě)SD卡中內(nèi)容的方法詳解
這篇文章主要介紹了Android數(shù)據(jù)持久化之讀寫(xiě)SD卡中內(nèi)容的方法,結(jié)合具體實(shí)例形式分析了Android持久化操作中針對(duì)SD卡進(jìn)行讀寫(xiě)操作的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-05-05
Android Studio 配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)步驟
本文主要介紹了Android Studio 配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Android監(jiān)聽(tīng)Home鍵實(shí)例詳解
這篇文章主要介紹了Android監(jiān)聽(tīng)Home鍵的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android編程實(shí)現(xiàn)監(jiān)聽(tīng)Home鍵的具體步驟與相關(guān)功能代碼,需要的朋友可以參考下2016-02-02
詳解Android中實(shí)現(xiàn)Redux方法
本篇文章給大家通過(guò)代碼實(shí)例教學(xué)Android中實(shí)現(xiàn)Redux的方法,有需要的朋友跟著參考下吧。2018-01-01
Android主項(xiàng)目與Module中R類的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Android主項(xiàng)目與Module中R類的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
解決Android Studio4.1沒(méi)有Gsonfomat插件,Plugin “GsonFormat” is inco
這篇文章主要介紹了解決Android Studio4.1沒(méi)有Gsonfomat插件,Plugin “GsonFormat” is incompatible (supported only in IntelliJ IDEA)的問(wèn)題 ,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12
Flutter本地存儲(chǔ)之基本的鍵值對(duì)存儲(chǔ)詳解
在原生的?Android?或?iOS?中,都提供了基本的鍵值對(duì)存儲(chǔ)方式,在?Flutter?中,提供了?shared_preferences?這個(gè)插件來(lái)實(shí)現(xiàn)本地鍵值對(duì)數(shù)據(jù)存儲(chǔ),本文就來(lái)和大家簡(jiǎn)單聊聊吧2023-03-03

