1.利用線性布局設定一個簡單的按鈕
<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"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- 定義一個按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示對話框"
android:layout_gravity="center_horizontal"
android:onClick="click"
/>
<!-- 定義一個單選按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示單選按鈕對話框"
android:layout_gravity="center_horizontal"
android:onClick="singleclick"
/>
</LinearLayout>
2.撰寫java代碼(MainActivity.java)檔案:
package com.example.dialogtest;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.AlteredCharSequence;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void singleclick(View v) {
//1、 創建AlertDialog. Builder物件, Builder類是AlertDi alog的內部類
AlertDialog. Builder builder = new AlertDialog . Builder(this);
//2.呼叫AlertDialog. Builder的方法為對話框設定圖示
builder.setIcon(R. drawable.ic_launcher); //設定圖示
//創建單選按鈕顯示的內容
String[] items ={"boy","girl"};
//builder.setSingleChoiceItems(irems,默認選擇了哪一個,當單擊要執行的事件)
builder.setSingleChoiceItems(items, 0, null);
//3、呼叫AlertDialog. Builder的create( )方法創建AlertDialog對話框
AlertDialog dialog = builder. create();
//4、呼叫AlertDialog的show( )方法顯示對話框
dialog. show();
}
public void click(View v){
//1、 創建AlertDialog. Builder物件, Builder類是AlertDi alog的內部類
AlertDialog. Builder builder = new AlertDialog . Builder(this);
//2.呼叫AlertDialog. Builder的方法為對話框設定圖示、標題、內容
builder.setIcon(R. drawable.ic_launcher); //設定圖示
builder.setTitle( "提示對話框");//設定標題
builder.setMessage( "我是你的小可愛呀! ! ");//設定內容
//為對話框設定一個"確定"按鈕 --第二個引數表示當前用戶選擇"確定"時要執行的事件
//當按下確定時執行類A中的onClick方法
builder.setPositiveButton("確定", new A());
//為對話框設定一個"取消"按鈕
builder.setNegativeButton("取消", new B());
//3、呼叫AlertDialog. Builder的create( )方法創建AlertDialog對話框
AlertDialog dialog = builder. create();
//4、呼叫AlertDialog的show( )方法顯示對話框
dialog. show();
}
//創建監聽器來監聽用戶按下確定鍵
@SuppressLint("ShowToast")
class A implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
// Toast Auto-generated method stub
//makeTest(this,要顯示的文本資訊,資訊顯示的時長)
//資訊時長有兩項選擇:1.Toast.LENGTH_SHORT 2.Toast.LENGTH_LONG
Toast toast = Toast.makeText(MainActivity.this, "您按下了確定", Toast.LENGTH_SHORT);
toast.show();
}
}
//創建監聽器來監聽用戶按下取消鍵
@SuppressLint("ShowToast")
class B implements OnClickListener{
@Override
public void onClick(DialogInterface arg0, int arg1) {
// Toast Auto-generated method stub
//makeTest(this,要顯示的文本資訊,資訊顯示的時長)
//資訊時長有兩項選擇:1.Toast.LENGTH_SHORT 2.Toast.LENGTH_LONG
Toast toast = Toast.makeText(MainActivity.this, "您按下了取消", Toast.LENGTH_SHORT);
//注意:MainActivity.this,為什么要加MainActivity的原因--
toast.show();
}
class C implements OnClickListener{
@Override
//第二個引數表示擁護單擊的是第幾個單選按鈕
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(which == 0){
//makeTest(this,要顯示的文本資訊,資訊顯示的時長)
//資訊時長有兩項選擇:1.Toast.LENGTH_SHORT 2.Toast.LENGTH_LONG
Toast toast = Toast.makeText(MainActivity.this, "您選擇了男性", Toast.LENGTH_SHORT);
toast.show();
}else{
//makeTest(this,要顯示的文本資訊,資訊顯示的時長)
//資訊時長有兩項選擇:1.Toast.LENGTH_SHORT 2.Toast.LENGTH_LONG
Toast toast = Toast.makeText(MainActivity.this, "您選擇了女性", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
uj5u.com熱心網友回復:
對話框建議使用DialogFragment實作轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/165418.html
標籤:Android
