国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 多種dialog的實(shí)現(xiàn)方法(推薦)

2019-12-12 01:13:09
字體:
供稿:網(wǎng)友

要求:設(shè)計(jì)如下界面,包括圖片按鈕、復(fù)選按鈕、單選按鈕、普通按鈕,單擊按鈕彈出對(duì)話框,運(yùn)行效果如下:

string.xml文件源代碼:

<resources>  <string name="app_name">多種彈出對(duì)話框</string>  <string name="dialog_img">圖片按鈕</string>  <string name="dialog_radio">單選按鈕</string>  <string name="dialog_box">復(fù)選按鈕</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="復(fù)選按鈕確定"/>  <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("您點(diǎn)擊了圖片按鈕!")            .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 = "";        //獲取復(fù)選按鈕的值        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();      }    });  }}

以上這篇Android 多種dialog的實(shí)現(xiàn)方法(推薦)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永年县| 浮梁县| 双峰县| 沙湾县| 赣榆县| 淳化县| 济阳县| 商南县| 阿拉尔市| 高碑店市| 虹口区| 蒙阴县| 清丰县| 宣城市| 乐安县| 稷山县| 县级市| 禹城市| 小金县| 大安市| 阳新县| 精河县| 石台县| 溆浦县| 佛山市| 庆阳市| 吴忠市| 新宾| 云南省| 萨迦县| 黎平县| 紫阳县| 连州市| 西华县| 洮南市| 沙湾县| 娱乐| 康保县| 石阡县| 南澳县| 瑞金市|