欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之AlertDialog實現(xiàn)彈出對話框

 更新時間:2022年09月19日 14:12:40   作者:ShadyPi  
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之AlertDialog實現(xiàn)彈出對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android開發(fā)之AlertDialog實現(xiàn)彈出對話框的具體代碼,供大家參考,具體內(nèi)容如下

基本框架

我們在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">

? ? <Button
? ? ? ? android:text="顯示對話框"
? ? ? ? android:onClick="display"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>

</LinearLayout>

然后在java代碼中編寫點擊事件的響應(yīng):

package com.example.myalertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }

? ? public void display(View view) {
? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}

構(gòu)造方法

先聲明一個構(gòu)造器對象builderAlertDialog.Builder builder=new AlertDialog.Builder(this);

之后就可以用該構(gòu)造器的各種方法設(shè)置對話框的屬性:

builder.setIcon(int iconId); 添加圖標(biāo)
builder.setMessage(CharSequence message); 添加消息
builder.setTitle(CharSequence title); 添加標(biāo)題
builder.setView(View view); 設(shè)置自定義布局
builder.create(); 創(chuàng)建對話框
builder.show(); 顯示對話框

上面的這些函數(shù)都是可以鏈?zhǔn)秸{(diào)用的(詳見基本框架),不過由于setXXX是Builder函數(shù),create函數(shù)返回Dialog變量,而show是void函數(shù),所以這三類函數(shù)的順序不能交換,setXXX函數(shù)的內(nèi)部順序可交換。

運行基本框架中的代碼就可以得到一個簡單的彈出對話框:

添加按鈕

常見的對話框一般還有按鈕,包含確認(rèn)、取消等按鈕,我們也可以在java代碼中設(shè)置并聲明點擊事件:

package com.example.myalertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }

? ? public void display(View view) {
? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊確認(rèn)");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊取消");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊中性");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}

點擊對應(yīng)按鈕可以看到事件被觸發(fā):

這三個按鈕可以根據(jù)自己的需要進行取舍與設(shè)置。

設(shè)置自定義布局

layout文件夾中新建資源文件:

隨便添加一點ImageView和TextView:

<?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:background="@color/purple_500"
? ? android:orientation="horizontal">

? ? <ImageView
? ? ? ? android:src="@mipmap/ic_launcher"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>

? ? <TextView
? ? ? ? android:text="Android"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>

</LinearLayout>

在java代碼中利用該資源文件生成一個View:

View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);

之后就可以將對話框的步距設(shè)置為該View了:

.setView(dialog_view)

MainActivity.java完整代碼:

package com.example.myalertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }

? ? public void display(View view) {

? ? ? ? View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);

? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊確認(rèn)");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊取消");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊中性");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setView(dialog_view)
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論