Android应用之基本的组件(一)
请大家伙多多指教:
请关注:ailiandeziwei
总的页面:
注意:按钮间方法的改变需要: 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 DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this).setMessage("您点击了确认按钮,文件已经被创建").show();
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface 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 DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface 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 DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface 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 DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "亲,你选中的科目是:"+items[which], Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "亲,你喜欢的科目有:", Toast.LENGTH_LONG).show();
}
}).show();
}
8.点击进度条
public void ProgressDialog(View v){
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case PRO:
if(PRO>=MAX_PROGRESS){
//重新设置
progress=0;
progressDialog.dismiss();//销毁对话框
}else{
progress++;
progressDialog.incrementProgressBy(1);
//延迟发送消息
handler.sendEmptyMessageDelayed(PRO,100);
}
break;
default:
break;
}
}
};
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.mw);
progressDialog.setTitle("正在处理数据......");
progressDialog.setMessage("请稍后.....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //设置进度条对话框 (水平,旋体)
//设置最大值
progressDialog.setMax(MAX_PROGRESS);
progressDialog.setButton("暂停",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.removeMessages(PRO);
}
});
progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//删除消息队列
handler.removeMessages(PRO);
//恢复进度初始值
progress=0;
progressDialog.setProgress(progress);
}
});
//显示
progressDialog.show();
//必须设置到show之后,show之前可能bug
progress = (progress>0) ?progress:0;
progressDialog.setProgress(progress);
//线程
handler.sendEmptyMessage(PRO);
}
还有一种效果是:
9.点击自定义表对话框
public void MyDialog(View v){
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.activity_main, null); //R.layout.activty_main自定义的布局文件这里可以是自己随意定义的
new AlertDialog.Builder(this).setView(view).setTitle("自定义的对话框").setPositiveButton("确认按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//处理
}
}).show();
}