您的位置:首页 > 移动开发 > Android开发

android中的ProgressDialog(进度),DatePickerDialog(日期选择)与TimePickerDialog(时间选择)

2016-10-24 11:10 891 查看

ProgressDialog

AlertDialog的子类,并且有ProgressBar的控件

所以包含他们两个的方法

1. setMax,setProgress

2. get…

3. setTitle,setIcon,show,dismiss,

4. setProgressStyle 设置形状

5. setCancelable

DatePickerDialog与TimePickerDialog

DatePicker与TimePicker一样都含有Dialog的方法




public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void progress1(View v) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("提示");
dialog.setMessage("正在加载");
// 设置为水平样式
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
new Thread() {
public void run() {
int index = 0;
while (index++ < dialog.getMax()) {
dialog.setProgress(index);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
// 关闭时一般判断
//
// if (dialog != null && dialog.isShowing())
// dialog.dismiss();
}

public void datePicker(View v) {
DatePickerDialog dialog = new DatePickerDialog(this,
new OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 这里是用户选择完毕后的回调
Toast.makeText(
getBaseContext(),
year + "-" + (monthOfYear + 1) + "-"
+ dayOfMonth, Toast.LENGTH_SHORT)
.show();

}
}, 2016, 11, 11);
dialog.show();
}

public void timePicker(View v) {
TimePickerDialog dialog = new TimePickerDialog(this,
new OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Toast.makeText(getBaseContext(),
hourOfDay + ":" + minute, Toast.LENGTH_SHORT)
.show();
}
}, 11, 11, true);
dialog.show();
}
}


xml的设置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="progress1"
android:text="进度对话框" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="datePicker"
android:text="日期选择器" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="timePicker"
android:text="时间选择器" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 控件
相关文章推荐