Java代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
Dialog dialog;
public void btnClick(View v){
LayoutInflater inflater = LayoutInflater.from(this);
View myView = inflater.inflate(R.layout.layout_myself_dialog, null); //自定义布局
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(myView);
myView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(MainActivity3.this,"点击了确定",Toast.LENGTH_LONG).show();
}
});
dialog = builder.create();
dialog.show();
}
}
界面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.srq.appch5.MainActivity3">
<Button
android:id="@+id/btn_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="对话框"
android:layout_centerInParent="true"
android:onClick="btnClick"
/>
</RelativeLayout>
自定义布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#0e2438"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提醒"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="恭喜您,验证码发送成功"
android:textColor="#000"
android:textSize="18sp" />
<Button
android:id="@+id/btn"
android:layout_width="66dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/btn_selector"
android:text="确定"
android:textColor="#fff"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<activity android:name=".MainActivity3"> //改这里
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
弹出窗
Java代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
PopupWindow popup;
public void btnClick(View v){
LayoutInflater inflater = LayoutInflater.from(this);
View myView = inflater.inflate(R.layout.layout_myself_dialog, null);
popup = new PopupWindow(myView,600,550); //单位是像素px
myView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
Toast.makeText(MainActivity3.this,"点击了确定",Toast.LENGTH_LONG).show();
}
});
// popup.showAsDropDown(v); //下拉式显示方式
popup.showAtLocation(v, Gravity.CENTER,0,0); //0是偏移量 //定点弹出
}
}
自定义布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#55ffffff"
>
<!--android:background="#fff" 不设背景色能看到后面按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#0e2438"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提醒"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="恭喜您,验证码发送成功"
android:textColor="#000"
android:textSize="18sp" />
<Button
android:id="@+id/btn"
android:layout_width="66dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/btn_selector"
android:text="确定"
android:textColor="#fff"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
类似对话框,不抢夺焦点
创建新的Java Class -- DialogActivity
public class DialogActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_myself_dialog);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.srq.appch5">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog"/>
<activity android:name=".DetailActivity" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
java代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
public void btnClick(View v){
//打开活动
Intent intent = new Intent(this,DialogActivity.class);
startActivity(intent); //手动创建要注册
}
}
布局和自定义布局内容不变
是一种Activity,很像对话框
注:改变风格要适当调整背景色,这里提前设定背景色为白色,否则风格改变,背景变黑色
日期选择对话框(引用java.until包)
public void btnClick(View v) {
Calendar c = Calendar.getInstance();
// 直接创建一个DatePickerDialog对话框实例,并将它显示出来
new DatePickerDialog(this,
// 绑定监听器
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int year,
int month, int dayOfMonth) {
String text = "您选择了:" + year + "年" + (month + 1)
+ "月" + dayOfMonth + "日";
Toast.makeText(MainActivity7.this, text,
Toast.LENGTH_LONG).show();
}
}
//设置初始日期
, c.get(Calendar.YEAR)
, c.get(Calendar.MONTH)
, c.get(Calendar.DAY_OF_MONTH)).show();
}
Java完整代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
public void btnClick(View v){
Calendar c = Calendar.getInstance();
// 直接创建一个DatePickerDialog对话框实例,并将它显示出来
new DatePickerDialog(this,
// 绑定监听器
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker dp, int year,
int month, int dayOfMonth) {
String text = "您选择了:" + year + "年" + (month + 1)
+ "月" + dayOfMonth + "日";
Toast.makeText(MainActivity3.this, text,
Toast.LENGTH_LONG).show();
}
}
//设置初始日期
, c.get(Calendar.YEAR)
, c.get(Calendar.MONTH)
, c.get(Calendar.DAY_OF_MONTH)).show();
}
}
时间选择对话框
public void btnClick(View v) {
Calendar c = Calendar.getInstance();
// 创建一个TimePickerDialog实例,并把它显示出来。
new TimePickerDialog(this,
// 绑定监听器
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker tp, int hourOfDay,
int minute) {
String text = "您选择了:" + hourOfDay + "时" + minute
+ "分";
Toast.makeText(MainActivity7.this, text,
Toast.LENGTH_LONG).show();
}
}
//设置初始时间
, c.get(Calendar.HOUR_OF_DAY)
, c.get(Calendar.MINUTE)
//true表示采用24小时制
, true).show();
}
Java完整代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
public void btnClick(View v){
Calendar c = Calendar.getInstance();
// 创建一个TimePickerDialog实例,并把它显示出来。
new TimePickerDialog(this,
// 绑定监听器
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker tp, int hourOfDay,
int minute) {
String text = "您选择了:" + hourOfDay + "时" + minute
+ "分";
Toast.makeText(MainActivity3.this, text,
Toast.LENGTH_LONG).show();
}
}
//设置初始时间
, c.get(Calendar.HOUR_OF_DAY)
, c.get(Calendar.MINUTE)
//true表示采用24小时制
, true).show();
}
}
Java代码
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3); }
ProgressDialog pd; public void showProgress(){
pd = new ProgressDialog(this);
pd.setTitle("任务执行中");
pd.setMessage("请稍后...");
pd.setCancelable(true); //可取消
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); //默认转圈
pd.show();
} public void btnClick(View v){
showProgress();
}}
UI线程不能放耗时操作 → 子线程(转圈实现:Java代码)
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
ProgressDialog pd;
public void showProgress(){
pd = new ProgressDialog(this);
pd.setTitle("任务执行中");
pd.setMessage("请稍后...");
pd.setCancelable(true); //可取消
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); //默认转圈
pd.show();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) { //主线程(UI)
pd.dismiss();
}
};
public void btnClick(View v){
showProgress();
//新建一个子线程
new Thread(){
@Override
public void run() {
for (int i = 0; i < 3; i++){ //强制阻塞线程,总共耗时3s(人为耗时操作)
try {
Thread.sleep(1000); //1000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
handler.sendEmptyMessage(0); //发送消息 子线程向主线程
}
}.start();
}
}
水平进度条实现(Java代码)
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
}
ProgressDialog pd;
public void showProgress(){
pd = new ProgressDialog(this);
pd.setTitle("任务执行中");
pd.setMessage("请稍后...");
pd.setCancelable(true); //可取消
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //水平进度条
pd.setMax(10);
pd.show();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) { //主线程(UI)
if (msg.what == 0){
pd.dismiss();
}else if (msg.what == 1){
pd.setProgress(msg.arg1);
}
}
};
public void btnClick(View v){
showProgress();
//新建一个子线程
new Thread(){
@Override
public void run() {
for (int i = 1; i <= 10; i++){ //强制阻塞线程,总共耗时3s(人为耗时操作)
try {
Thread.sleep(1000); //1000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = Message.obtain();
msg.arg1 = i; //把增长的进度放在消息里,携带过去
msg.what = 1;
handler.sendMessage(msg); //发送实消息
}
handler.sendEmptyMessage(0); //what = 0;
}
}.start();
}
}
两次点击退出
private long mExitTime;
@Override
public void onBackPressed() { if ((System.currentTimeMillis() - mExitTime) > 2000) {
Toast.makeText(this, "再按一次退出",
Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
}
Java代码(不全,加在最后即可)
private long mExitTime;
@Override
public void onBackPressed() {
if ((System.currentTimeMillis() - mExitTime) > 2000) { //退出时间和系统时间的差值在两秒之内,退出
Toast.makeText(this, "再按一次退出",
Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis(); //大于2s不退出,更新退出时间
} else {
finish();
}
}