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)文章
詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android數(shù)據(jù)持久化之讀寫SD卡中內(nèi)容的方法詳解
這篇文章主要介紹了Android數(shù)據(jù)持久化之讀寫SD卡中內(nèi)容的方法,結(jié)合具體實例形式分析了Android持久化操作中針對SD卡進(jìn)行讀寫操作的相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下2017-05-05
Android Studio 配置國內(nèi)鏡像源的實現(xiàn)步驟
本文主要介紹了Android Studio 配置國內(nèi)鏡像源的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
解決Android Studio4.1沒有Gsonfomat插件,Plugin “GsonFormat” is inco
這篇文章主要介紹了解決Android Studio4.1沒有Gsonfomat插件,Plugin “GsonFormat” is incompatible (supported only in IntelliJ IDEA)的問題 ,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12

