多種Dialog對話框——Android開發(fā)

要求:設(shè)計(jì)如下界面躺翻,包括圖片按鈕鳞芙、復(fù)選按鈕眷柔、單選按鈕、普通按鈕原朝,單擊按鈕彈出對話框驯嘱,運(yùn)行效果如下:
初始界面

點(diǎn)擊圖片按鈕

點(diǎn)擊復(fù)選按鈕

點(diǎn)擊單選按鈕

string.xml文件源代碼:

<resources>
    <string name="app_name">多種彈出對話框</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();
            }
        });
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末喳坠,一起剝皮案震驚了整個濱河市鞠评,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌壕鹉,老刑警劉巖剃幌,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件聋涨,死亡現(xiàn)場離奇詭異,居然都是意外死亡负乡,警方通過查閱死者的電腦和手機(jī)牍白,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抖棘,“玉大人茂腥,你說我怎么就攤上這事∏惺。” “怎么了最岗?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長朝捆。 經(jīng)常有香客問我般渡,道長,這世上最難降的妖魔是什么右蹦? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任诊杆,我火速辦了婚禮,結(jié)果婚禮上何陆,老公的妹妹穿的比我還像新娘晨汹。我一直安慰自己,他們只是感情好贷盲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布淘这。 她就那樣靜靜地躺著,像睡著了一般巩剖。 火紅的嫁衣襯著肌膚如雪铝穷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天佳魔,我揣著相機(jī)與錄音曙聂,去河邊找鬼。 笑死鞠鲜,一個胖子當(dāng)著我的面吹牛宁脊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贤姆,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼榆苞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了霞捡?” 一聲冷哼從身側(cè)響起坐漏,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赊琳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體街夭,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年慨畸,在試婚紗的時候發(fā)現(xiàn)自己被綠了莱坎。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衣式。...
    茶點(diǎn)故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寸士,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出碴卧,到底是詐尸還是另有隱情弱卡,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布住册,位于F島的核電站婶博,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏荧飞。R本人自食惡果不足惜凡人,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望叹阔。 院中可真熱鬧挠轴,春花似錦、人聲如沸耳幢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽启上。三九已至,卻和暖如春店印,著一層夾襖步出監(jiān)牢的瞬間冈在,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工按摘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留包券,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓院峡,卻偏偏與公主長得像兴使,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子照激,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容