Android应用之基本的组件(一)
总的页面:
注意:按钮间方法的改变需要: android:onClick="clearNoti" 添加相应的方法即可
1.点击状态栏按钮时:
public void Notification(View v){
showNotification("亲来短信了","5557","我喜欢你", R.drawable.ic_launcher, R.drawable.ic_launcher);
}
public void showNotification(String tickerText,String contentTitle,String contentText ,int iconId,int notiId ){
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建一个Notification
Notification notification = new Notification();
//设置发出信息的内容
notification.icon =iconId;
//设置发出的信息
notification.tickerText=tickerText;
//设置发出通知的时间
notification.when=System.currentTimeMillis();
//设置显示通知的默认的发声或者振动,Light效果
notification.defaults=Notification.DEFAULT_VIBRATE;//振动的效果
//3步: PendingIntent Android系统负责维护
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent() ,0);
//Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis());
//4步;设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle,contentText,pendingIntent);
//5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定 Notification的标识
notificationManager.notify(notiId,notification);
} 效果图:
2.点击清楚状态按钮时:
//清除的操作
public void clearNoti(View v ){
notificationManager.cancelAll();//清除所有
}
3点击创建对对话框时: public void DiaLog(){
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.mw)
.setTitle("DiaLog").setMessage("是否创建文件")
.setPositiveButton("确认",new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
new AlertDialog.Builder(MainActivity.this).setMessage("您点击了确认按钮,文件已经被创建").show();
}
})
.setNegativeButton("取消",new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("您已经选择了取消的按钮,该文件不会被创建").create()
.show();
}
}).show();
}
4。点击简单列表对话框
public void Simplelistdialog(View v){
final String items[] = {"java","php","3g",".net"};
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("简单列表对话框").setItems(items, new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
Toast.makeText(getApplicationContext(), "亲,你喜欢的科目是:"+items[which],Toast.LENGTH_LONG).show();
}
}).show();
}
5.点击单选列表对话框
public void Radiolistdialog(View v){
final String items[] = {"java","php","3g",".net"};
AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("单选列表对话框")
//.setSingleChoiceItems(items, checkedItem, listener)
// .setSingleChoiceItems(itemsId, checkedItem, listener)
//.setSingleChoiceItems(cursor, checkedItem, labelColumn, listener) labelColumn如果数据源是数据集
//数据集中的某一列会作为列表对话框的数据加载的列表框中,该参数表示该列的名称(字段名称)
.setSingleChoiceItems(items,1, new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
Toast.makeText(getApplicationContext(), "亲,你喜欢的科目是:"+items[which],Toast.LENGTH_LONG).show();
}
}).show();
}
7.点击多选列表对话框
public void Multiselectlistdialog(View v){
final String items[]={"Java","PHP","3G",".NET"};
new AlertDialog.Builder(this).setTitle("多选列表对话框")
//.setMultiChoiceItems(itemsId, checkedItems, listener)
//.setMultiChoiceItems(cursor, isCheckedColumn, labelColumn, listener)
.setMultiChoiceItems(items, new boolean[]{false,true,true,false}, new DialogInte易做图ce.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "亲,你选中的科目是:"+items[which], Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton("确认", new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
Toast.makeText(getApplicationContext(), "亲,你喜欢的科目有:", Toast.LENGTH_LONG).show();
}
}).show();
}