自定义Dialog可以让弹出的Dialog对话框以我们自定义的形式显示。
①首先我们在values->styles.xml中添加一个去除默认Dialog格式的style。
<!-- 去除Dialog默认格式 -->
<style name="NoDialogTitle" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
②创建Dialog对象,传入该style name。
Dialog dialog=new Dialog(MainActivity.this,R.style.NoDialogTitle);
③因为去除了默认格式,所以我们要为Dialog设置我们自己的格式,另外我们还要自定义布局。
在drawable文件夹下新建xml文档:
corners android:radius 设置边缘弧度
corners android:topLeftRadius 设置左上角弧度
solid android:color 设置颜色
stroke android:color 设置边框颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp" />
<solid android:color="#FAF9E0" />
</shape>
新建自定义布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/dialog">
<TextView android:id="@+id/textview_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textColor="#ff0000"/>
<TextView android:id="@+id/textview_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:id="@+id/button_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/>
<Button android:id="@+id/button_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/>
</LinearLayout>
</LinearLayout>
MainActivity
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
public class MainActivity extends Activity {
private Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_mdialog = (Button) findViewById(R.id.button_mdialog);
button_mdialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog=new Dialog(MainActivity.this,R.style.NoDialogTitle);
LayoutInflater inflater=getLayoutInflater();
//加载自定义布局
View view =inflater.inflate(R.layout.mydialog, null);
TextView tv_title=(TextView) view.findViewById(R.id.textview_title);
tv_title.setText("这是一个标题");
TextView tv_message=(TextView) view.findViewById(R.id.textview_message);
tv_message.setText("这是内容");
Button button_cancel = (Button) view.findViewById(R.id.button_cancel);
button_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button button_ok =(Button) view.findViewById(R.id.button_ok);
button_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "您点击了确定按钮", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.setContentView(view);
dialog.setCancelable(false);
dialog.show();
}
});
}
}
DatePickerDialog是系统的一个选择日期的对话框。创建DatePickerDialog对象,调用show()方法即可。
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import android.app.Activity;
import android.app.DatePickerDialog;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends Activity {
private Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_date=(Button) findViewById(R.id.button_datedialog);
button_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar=Calendar.getInstance();
DatePickerDialog dialog=new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar.set(year, monthOfYear,dayOfMonth);
SimpleDateFormat format= new SimpleDateFormat("yyyy年MM月dd日");
Toast.makeText(getApplicationContext(), "选择的日期为:"+format.format(calendar.getTime()), Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
});
}
}
运行结果:点击按钮,弹出日期选择对话框,选择2016年1月11日
点击完成,短暂显示选择的日期
TimePickerDialog是系统的时间选择对话框。
用法与DatePickerDialog一样,这里不再做详细介绍。
PopupWindow是一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部。
setWidth(ViewGroup.LayoutParams.MATCH_PARENT); 设置弹出窗口的宽度
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);设置弹出窗口的高度
setContentView(view); 设置窗口的自定义布局
setOutsideTouchable(true); 设置点击窗口外部,是否可消失
showAsDropDown(); 设置出现在某个控件下边
首先我们要自定义窗口的布局popupwindow_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/textview_pop1" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/textview_pop2" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/textview_pop3" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private Button button_pop;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_pop = (Button) findViewById(R.id.button_popup);
button_pop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow = new PopupWindow(MainActivity.this);
View view = getLayoutInflater().inflate(
R.layout.popupwindow_layout, null);
TextView tv_pop1 = (TextView) view
.findViewById(R.id.textview_pop1);
tv_pop1.setText("我是第一条内容");
TextView tv_pop2 = (TextView) view
.findViewById(R.id.textview_pop2);
tv_pop2.setText("我是第二条内容");
TextView tv_pop3 = (TextView) view
.findViewById(R.id.textview_pop3);
tv_pop3.setText("我是第三条内容");
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(button_pop);
}
});
}
/** *重写点击返回按钮的方法,点击返回按钮,让对话框消失! */
@Override
public void onBackPressed() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
super.onBackPressed();
}
}
}
Notification,顾名思义通知,用于在设备状态栏弹出一些短小的通知信息。
我们在主界面给两个按钮
<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" tools:context=".MainActivity" >
<Button android:id="@+id/button_creatNotification" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹出通知"/>
<Button android:id="@+id/button_cancelNotification" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消通知"/>
</LinearLayout>
给跳转的Activity一个布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="天猫打折界面" android:gravity="center"/>
</LinearLayout>
创建跳转的Activity,记得注册!
import android.app.Activity;
import android.os.Bundle;
public class DiscountActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.discount_layout);
}
}
MainActivity
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化NotificationManager
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Button button_creat = (Button) findViewById(R.id.button_creatNotification);
button_creat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = new Notification();
notification.icon = R.drawable.cxhd;//设置通知图片
notification.tickerText = "天猫打折啦!";//设置状态栏文本
notification.flags = Notification.FLAG_AUTO_CANCEL;//设置可自动取消
Intent intent = new Intent(MainActivity.this, DiscountActivity.class);//设置PendingIntent事件
PendingIntent pending = PendingIntent.getActivity(
MainActivity.this, 1, intent,
PendingIntent.FLAG_ONE_SHOT);//设置PendingIntent使用方式
notification.setLatestEventInfo(MainActivity.this, "天猫",
"年终打折,各种礼品相送", pending);
notification.when = System.currentTimeMillis();// 或者Calendar.getInstance().getTimeInMillis();
notificationManager.notify(1, notification);//将通知添加到管理器
}
});
Button button_cancel = (Button) findViewById(R.id.button_cancelNotification);
button_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notificationManager.cancel(1);//取消管理器中的第一条通知
}
});
}
}
运行结果:点击弹出通知:
下拉状态栏,显示通知内容:
点击,跳转到另一界面:
另外,点击取消通知按钮,通知消失。或者直接在通知栏中滑动,取消通知。
上面写的通知方法已经过时,被Notification.Builder代替。
Intent intent = new Intent(MainActivity.this , DiscountActivity.class);
PendingIntent pending = PendingIntent.getActivity(
MainActivity.this, 1, intent,
PendingIntent.FLAG_ONE_SHOT);
Notification notify = new Notification.Builder(this)
.setSmallIcon(R.drawable.cxhd)
.setTicker("天猫打折啦!")
.setContentTitle("天猫")
.setContentText("年终打折,各种礼品相送")
.setContentIntent(pending)
.setAutoCancel(true);
.setWhen(System.currentTimeMillis());
.getNotification();
注意:getNotification()方法是在API 11下使用,在API 16中此方法被build()方法代替。
首先我们要新建一个xml文档,自定义我们的Noftification布局,然后需要创建一个RemoteViews对象,需要注意的是RemoteViews中传入的布局控件只能是TextView,ImageView和ProgressBar。最后调用setContent()方法即可,其他的代码是一样的。
RemoteVeiws remoteViews = new RemoteViews(getPackageName(),R.layout.自定义布局);
.setContent(remoteViews);