/**
* 本代码主要演示 对话框的操作
*/
package haha.android.HelloWorld4;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloWorld4Activity extends Activity {
Button btn1, btn2, btn3,btn4,btn5 ;
//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hahaLog("进入main activity");
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new ButtonListener());
btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(new ButtonListener());
btn3 = (Button)findViewById(R.id.button3);
btn3.setOnClickListener(new ButtonListener());
btn4 = (Button)findViewById(R.id.button4);
btn4.setOnClickListener(new ButtonListener());
btn5 = (Button)findViewById(R.id.button5);
btn5.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener
{
final String[] listStr={"A","B","C","D","E","F","G","H","I","J","K"};
public void onClick (View v)
{
switch(v.getId())
{
case R.id.button1:
hahaLog("进入button1");
final AlertDialog.Builder ab1 = new AlertDialog.Builder(HelloWorld4Activity.this);
ab1.setTitle("简单对话框");
ab1.setIcon(android.R.drawable.ic_dialog_info);
ab1.setMessage("这是消息内容!\n点击ok连续popup.");
ab1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button1的 Pos click");
ab1.show();
}
});
ab1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button1的 Neg click");
}
});
ab1.show();
break;
case R.id.button2:
hahaLog("进入button2");
final AlertDialog.Builder ab2 = new AlertDialog.Builder(HelloWorld4Activity.this);
ab2.setTitle("简单LIST对话框");
ab2.setIcon(android.R.drawable.ic_dialog_info);
//ab2.setMessage("这是消息内容!\n点击ok连续popup.");//加了这个,就没有LIST了,显示为空白
ab2.setItems(listStr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button2的click");
Toast.makeText(getApplicationContext(),
"你点击的是:"+listStr[which],
Toast.LENGTH_LONG).show();
}
});
// ab2.create();
ab2.show();
break;
case R.id.button3:
hahaLog("进入button3");
String[] mstr = {"aaa","bbbbb","ccccc"};
final boolean[] bb = {false,true,false};//指向的地址不能改变,不是内容不能改变
final AlertDialog.Builder ab3 = new AlertDialog.Builder(HelloWorld4Activity.this);
ab3.setTitle("简单radio的LIST对话框");
ab3.setIcon(android.R.drawable.ic_dialog_info);
if(false){
ab3.setItems(listStr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button2的click");
Toast.makeText(getApplicationContext(),
"你点击的是:"+listStr[which],
Toast.LENGTH_LONG).show();
}
});
ab3.setSingleChoiceItems(listStr, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button3的click,which="+which);
Toast.makeText(getApplicationContext(),
"你点击的是:"+listStr[which],
Toast.LENGTH_LONG).show();
}});
}else{
ab3.setMultiChoiceItems(mstr, bb, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
}
});
/* ab3.setItems(listStr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button2的click");
Toast.makeText(getApplicationContext(),
"你点击的是:"+listStr[which],
Toast.LENGTH_LONG).show();
}
});*/
}
//public AlertDialog.Builder setCancelable (boolean cancelable)参考代码有设置这个属性为true,我没设置,一样ok,默认就是ture?...
ab3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hahaLog("进入button3的 Neg click");
System.out.println("haha:" + "bb={" + bb[0] + "," + bb[1]+ "," + bb[2]+ "}");
}
});
// ab3.create();
ab3.show();
break;
case R.id.button4:
hahaLog("进入button4");
final ProgressDialog progressDialog = ProgressDialog.show(HelloWorld4Activity.this,
"Loading","please wait…",true);
progressDialog.setCancelable(true);//这里要设置为TRUE,默认是不能cancel该对话框的
break;
case R.id.button5:
hahaLog("进入button5");
LayoutInflater inflater = (LayoutInflater)(getApplicationContext().getSystemService
(getApplicationContext().LAYOUT_INFLATER_SERVICE));
View view=inflater.inflate(R.layout.seldef, null);
AlertDialog.Builder builder2=new AlertDialog.Builder(HelloWorld4Activity.this);
builder2.setView(view);
builder2.setTitle("qq title");
builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//这里还有
dialog.cancel();
}
});
//builder2.create();
builder2.show();
break;
default:
break;
}
}
} //class ButtonListener
void hahaLog(String msg)
{
//Log.d("haha", msg);
System.out.println("haha: " + msg);
}
}//class HelloWorld4Activity
/selfdef.xml//
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- 上面这个vertical必须要,不然只能显示出 image -->
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/ic_launcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="@+string/dlg_selfdef_user"/>
<EditText
android:id="@+id/EditView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="26"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/dlg_selfdef_pass"/>
<EditText
android:id="@+id/EditView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:password="true"
android:singleLine="true"
/>
</LinearLayout>
</LinearLayout>
///strings.xml/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld4Activity!</string>
<string name="app_name">HelloWorld4</string>
<string name="dlg_simple_popup">简单填出框</string>
<string name="dlg_simple_list_popup">简单LIST对话框</string>
<string name="dlg_radio_list_popup">简单radio的LIST对话框</string>
<string name="dlg_progress_list_popup">简单进度对话框</string>
<string name="dlg_selfdef_popup">自定义对话框</string>
<string name="dlg_selfdef_user">user:</string>
<string name="dlg_selfdef_pass">password:</string>
</resources>