今天看书写了这两个例子:一、对载提示框的使用,二、具有选择功能的提示框使用。
Activity例码:
package com.pdw2009;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
*
* @author peidw 2009-08-23
*
*/
public class ProgressDemoActivity extends Activity{
public ProgressDialog myDialog=null;
private TextView tv_1;
private Button btn_1;
private Button btn_2;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.progress);
btn_1=(Button)this.findViewById(R.id.btn_progress1);
btn_2=(Button)this.findViewById(R.id.btn_selectlert);
tv_1=(TextView)this.findViewById(R.id.tv_1);
btn_1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
myDialog=ProgressDialog.show(ProgressDemoActivity.this, "加载","加载中,请稍候..",true);
tv_1.setText("加载中,请稍候….");
new Thread(){
public void run(){
try{
sleep(3000);
}catch(Exception e){
e.printStackTrace();
}finally{
myDialog.dismiss();
}
}
}.start();
}
});
btn_2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
new AlertDialog.Builder(ProgressDemoActivity.this)
.setTitle("请选择")
.setItems(R.array.sel_fruit, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
String ary_fruit[]=getResources().getStringArray(R.array.sel_fruit);
String sel_value=ary_fruit[which];
new AlertDialog.Builder(ProgressDemoActivity.this)
.setMessage("你选择的是: "+sel_value)
.setNegativeButton("确认", new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which) {
// TODO 确认选择项时要做的操作
}
}).show();
}
}).show();
}
});
}
}
界面progress.xml代码
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_progress" android:layout_y="78dip" android:layout_x="82dip" android:id="@+id/btn_progress1">
</Button>
<TextView android:id="@+id/tv_1" android:layout_y="171dip" android:layout_width="wrap_content" android:text="TextView01" android:layout_height="wrap_content" android:layout_x="0px" android:hint="测试" android:height="20px"></TextView>
<Button android:layout_y="97dip" android:id="@+id/btn_selectlert" android:layout_width="wrap_content" android:layout_x="171dip" android:text="@string/select_alertdialog" android:layout_height="wrap_content"></Button>
</AbsoluteLayout>
string.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ExampleActivity!</string>
<string name="app_name">Example</string>
<string name="quit">退出</string>
<string name="app_about">请确认</string>
<string name="btn_ok">确定</string>
<string name="btn_progress">加载测试</string>
<string name="btn_alert">AlertDiadlog窗口</string>
<string name="select_alertdialog">具有选择功能的AD</string>
<string-array name="sel_fruit">
<item>苹果</item>
<item>龙眼</item>
<item>桃子</item>
<item>荔枝</item>
</string-array>
</resources>
相关图片