安卓学习-界面-ui-DatePickerDialog、TimePickerDialog和ProgressDialog
1.DatePickerDialog和TimePickerDialog
例子
activity_main.xml
<RelativeLayout 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="${relativePackage}.${activityClass}" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignParentRight="true" android:layout_marginRight="80dp" android:text="时间" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginRight="50dp" android:layout_marginTop="32dp" android:layout_toLeftOf="@+id/button2" android:text="日期" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
TextView tv1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1=(Button)findViewById(R.id.button1);
Button btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
Calendar c=Calendar.getInstance();
@Override
public void onClick(View v) {
DatePickerDialog d=new DatePickerDialog(MainActivity.this,new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(MainActivity.this, "年"+year+":月"+(monthOfYear+1)+":日"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
d.show();
}
});
btn2.setOnClickListener(new OnClickListener() {
Calendar c=Calendar.getInstance();
@Override
public void onClick(View v) {
TimePickerDialog t=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO 自动生成的方法存根
Toast.makeText(MainActivity.this, "时"+hourOfDay+":分"+(minute+1), Toast.LENGTH_SHORT).show();
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
t.show();
}
});
}
}
2.ProgressDialog
activity_main.xml
<RelativeLayout 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="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="59dp" android:text="无进度条" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="33dp" android:text="带进度条" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="50dp" android:text="模拟耗时" /> </RelativeLayout>
MainActivity
public class MainActivity extends Activity { ProgressDialog p; int pos=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.button1); Button btn2=(Button)findViewById(R.id.button2); Button btn3=(Button)findViewById(R.id.button3); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ProgressDialog.show(MainActivity.this, "任务执行中","任务执行中,请等待......", true,false); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { p=new ProgressDialog(MainActivity.this); p.setTitle("任务执行中"); p.setMessage("任务执行中,请等待......"); //可以被取消 p.setCancelable(true); //样式 //滚动 ProgressDialog.STYLE_SPINNER //水平进度 STYLE_HORIZONTAL p.setProgressStyle(ProgressDialog.STYLE_SPINNER); p.setIndeterminate(true); p.show(); } }); final Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==1234){ p.setProgress(pos); if(pos==100){ Toast.makeText(MainActivity.this, "完成", Toast.LENGTH_SHORT).show(); } } } }; btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { p=new ProgressDialog(MainActivity.this); p.setMax(100); p.setMax(0); p.setTitle("任务执行中"); p.setMessage("任务执行完成比,请等待......"); p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); p.setCancelable(false); p.setIndeterminate(false); p.show(); new Thread(){ public void run() { try { while(pos<=100){ pos=pos+1; Message msg=new Message(); msg.what=1234; handler.sendMessage(msg); sleep(100); } p.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } }); } }