為什么我的是一片空白呢?
package com.sorgs.administrator.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
? ?String[] single_list = {"男", "女", "女博士", "程序員"};
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ?}
? ?//初始化點擊事件
? ?public void initEvent() {
? ? ? ?findViewById(R.id.dialog_btn1).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View view) {
? ? ? ? ? ? ? ?showDialog1();
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?findViewById(R.id.dialog_btn2).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View view) {
? ? ? ? ? ? ? ?showDialog2();
? ? ? ? ? ?}
? ? ? ?});
? ?}
? ?//顯示確認對話框
? ?private void showDialog1() {
? ? ? ?Builder builder = new Builder(this);
? ? ? ?builder.setTitle("確認對話框"); ?//設置標題
? ? ? ?builder.setIcon(R.mipmap.ic_launcher); ?//設置圖標
? ? ? ?builder.setMessage("確認對話框提示內容"); ?//設置內容
? ? ? ?builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this, "點擊了確定按鈕!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this, "點擊了取消按鈕!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?AlertDialog dialog = builder.create(); ?//獲取dialog
? ? ? ?dialog.show(); //顯示對話框
? ?}
? ?private void showDialog2() {
? ? ? ?Builder builder = new Builder(this);
? ? ? ?builder.setTitle("選擇性別"); ?//設置標題
? ? ? ?builder.setIcon(R.mipmap.ic_launcher); ?//設置圖標
? ? ? ?builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ?String str = single_list[i];
? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this, "這個人是"+str, Toast.LENGTH_SHORT).show();
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this, "點擊了確定按鈕!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ?}
? ? ? ?});
? ?}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?xmlns:tools="http://schemas.android.com/tools"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent"
? ?tools:context=".MainActivity"
? ?android:orientation="vertical">
? <Button
? ? ? android:id="@+id/dialog_btn1"
? ? ? android:layout_width="match_parent"
? ? ? android:layout_height="wrap_content"
? ? ? android:text="確認對話框"/>
? <Button
? ? ? ?android:id="@+id/dialog_btn2"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:text="單選按鈕對話框"/>
</LinearLayout>
2017-06-02
?AlertDialog dialog = builder.create(); ?//獲取dialog
? ? ? ?dialog.show(); //顯示對話框 第二個方法沒加這個。
2016-10-05
setContentView把布局文件引入啊,望采納
2016-08-19
既然解決了那采納我行不行;)
2016-08-18
我自己找到原因了。刪除沒用的代碼,把布局引入刪了