溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中怎么實現(xiàn)多種dialog

發(fā)布時間:2021-06-28 18:06:31 來源:億速云 閱讀:183 作者:Leah 欄目:移動開發(fā)

本篇文章為大家展示了Android中怎么實現(xiàn)多種dialog,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

string.xml文件源代碼:

<resources>
  <string name="app_name">多種彈出對話框</string>
  <string name="dialog_img">圖片按鈕</string>
  <string name="dialog_radio">單選按鈕</string>
  <string name="dialog_box">復選按鈕</string>
  <string name="close">關(guān)閉</string>
</resources>

布局文件源代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <ImageView
   android:id="@+id/img"
   android:layout_height="100dp"
   android:layout_width="100dp"
   android:src="@drawable/aa"/>
  <CheckBox
    android:id="@+id/Checkbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Chinese"/>
  <CheckBox
    android:id="@+id/Checkbox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="English"
    android:checked="true"/>
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="復選按鈕確定"/>
  <RadioGroup
    android:id="@+id/radioGroup"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton
      android:id="@+id/male"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="male" />
    <RadioButton
      android:id="@+id/female"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="female"/>
  </RadioGroup>
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="單選按鈕確定"/>
</LinearLayout>

java文件源代碼:

package com.example.shiyan1_4;
import android.app.Dialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import static android.R.attr.id;
public class Shiyan1_4Activity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shiyan1_4);
    ImageView img = (ImageView) findViewById(R.id.img);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    img.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog1 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        dialog1.setTitle(R.string.dialog_img)
            .setMessage("您點擊了圖片按鈕!")
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
    button1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog1 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        String str1 = "";
        //獲取復選按鈕的值
        CheckBox Checkbox1 = (CheckBox)findViewById(R.id.Checkbox1);
        if(Checkbox1.isChecked()){
          str1 += Checkbox1.getText() + "is selected !";
        }
        CheckBox Checkbox2 = (CheckBox)findViewById(R.id.Checkbox2);
        if(Checkbox2.isChecked()){
          str1 += Checkbox2.getText() + " is selected !\n";
        }
        dialog1.setTitle(R.string.dialog_box)
            .setMessage(str1)
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        AlertDialog.Builder dialog2 = new AlertDialog.Builder(Shiyan1_4Activity.this);
        String str2 = "";
        //獲取單選按鈕的值
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        for (int i = 0; i <radioGroup.getChildCount(); i++){
          RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
          if(radioButton.isChecked()){
            str2 += radioButton.getText() + " is selected !";
            break;
          }
        }
        dialog2.setTitle(R.string.dialog_radio)
            .setMessage(str2)
            .setNegativeButton(R.string.close, null)
            .create()
            .show();
      }
    });
  }
}

上述內(nèi)容就是Android中怎么實現(xiàn)多種dialog,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI