题目
请创建一个模块,设计多个页面,分别用到AlertDialog、ProgressDialog、DatePickerDialog等对话框, 能够用到动态管理的碎片Fragment。
效果图
一、开发主页面MainActivity
1.编写主页布局activity_main.xml
<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"
android:background="@drawable/img"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dip"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="综合应用案例"
android:gravity="center"
android:textColor="#00ff99"
android:textSize="40sp" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#00ff00"
android:text="AlertDialog" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="DatePickerDialog" />
</LinearLayout>
2.编写主页MainActivity.java
package com.example.week6homework;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = this.findViewById(R.id.button1);
button2 = this.findViewById(R.id.button2);
button1.getBackground().setAlpha(100);
button2.getBackground().setAlpha(100);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,AlertDialogand.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,DatePickerDialog.class);
startActivity(intent);
}
});
}
}
二、开发AlertDialog页面
1.编写activity_alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img"
tools:context=".AlertDialogand">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dip"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="AlertDialog案例"
android:gravity="center"
android:textColor="#00ff99"
android:textSize="40sp" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#00ff00"
android:text="AlertDialog"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="列表对话框"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="144dp"
android:text="进度条对话框"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.编写AlertDialog.java
package com.example.week6homework;
import java.util.Timer;
import java.util.TimerTask;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class AlertDialogand extends AppCompatActivity {
Button alert,list,progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
alert = (Button)findViewById(R.id.button1);
list = (Button)findViewById(R.id.button2);
progress = (Button)findViewById(R.id.button3);
alert.getBackground().setAlpha(100);
list.getBackground().setAlpha(100);
progress.getBackground().setAlpha(100);
alert.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
createAlertDialog().show();
}
});
list.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
createListDialog().show();
}
});
progress.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
createProgressDialog().show();
}
});
}
private AlertDialog createAlertDialog(){
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.img)
.setTitle("提示")
.setMessage("点击确定返回主页")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AlertDialogand.this.finish();
}
}).create();
return dialog;
}
private AlertDialog createListDialog(){
final String items[] = {"吃饭", "睡觉", "打豆豆", "摆烂"};
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.img)
.setTitle("选择您的兴趣爱好:")
.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogand.this, "您的选择:"+items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
return dialog;
}
private ProgressDialog createProgressDialog(){
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("正在加载中");
dialog.setMax(100);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int currentProgress = 0;
@Override
public void run() {
currentProgress+=10;
dialog.setProgress(currentProgress);
if (currentProgress>=100) {
timer.cancel();
dialog.dismiss();
}
}
}, 0, 1000);
return dialog;
}
}
三、开发DatePickerDialog页面
1.编写activity_date_picker_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img"
tools:context=".DatePickerDialog">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="120dip"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="DatePicker应用案例"
android:textColor="#00ff99"
android:textSize="40dp" />
</LinearLayout>
<TextView
android:id="@+id/c57DataTimeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text=""
android:textSize="18sp" />
<ScrollView
android:id="@+id/c57scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<DatePicker
android:id="@+id/c57datePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
2. 编写DatePickerDialog.java
package com.example.week6homework;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class DatePickerDialog extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
|