知识复习
dialog对话框
普通对话框
btn_bottom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("你好");
builder.setMessage("欢迎");
//点击返回按钮
builder.setCancelable(true);
//确定和取消
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击ok", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("中性", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击中兴", Toast.LENGTH_SHORT).show();
}
});
//创建对话框
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
单选对话框
//单选
btn_siggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("标题");//设置标题
final String[] str={"卡夫卡", "牛顿", "新年套"};
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
builder.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击"+str[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
多选对话框
//多选
btn_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//数据源
final String[] str={"ppt","c++","java"};
//判断
final boolean[] bol={true,false,true};
builder.setMultiChoiceItems(str, bol, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
bol[which]=isChecked;
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
时间对话框
btn_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Calendar calendar= Calendar.getInstance();//对象
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}
// },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
},3,6,true).show();
}
});
日期对话框
//日期对象
btn_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
自定义对话框
//自定义一部分
btn_lay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_1, null);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
//自定义全部
btn_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final mydialog1 mydialog1 = new mydialog1(MainActivity.this);
mydialog1.show();
mydialog1.setTvClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mydialog1.changetv("didididi");
}
});
}
});
菜单
系统菜单
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.tv_show);
}
//重写加载资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.create,menu);
// menu.add(1,1,1,"111");
// menu.add(1,2,1,"222");
return true;
}
//重写事件监听
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:
Toast.makeText(this, "变", Toast.LENGTH_SHORT).show();
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onOptionsItemSelected(item);
}
}
上下文菜单
public class Context_Activity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_);
textView=findViewById(R.id.tv_show);
//为控件添加长按属性
registerForContextMenu(textView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.create,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onContextItemSelected(item);
}
}
弹出菜单
public class out_Menu extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_out__menu);
textView=findViewById(R.id.tv_show);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建方法
showPopupMenu();
}
});
}
private void showPopupMenu() {
//创建对象
//参数一 上下文 参数二 菜单显示在指定控件的下方
PopupMenu popupMenu = new PopupMenu(this, textView);
//记载布局
popupMenu.inflate(R.menu.create);
//监听器
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return false;
}
});
//显示
popupMenu.show();
}
}
弹出窗口
public class Popup_window extends AppCompatActivity {
private TextView popup_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
popup_tv=findViewById(R.id.tv_popup);
popup_tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPopwindows();
}
});
}
public void setPopwindows(){
View view = LayoutInflater.from(this).inflate(R.layout.item, null);
PopupWindow popupWindow = new PopupWindow(view, 200, 100, true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(popup_tv, Gravity.BOTTOM,0,0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha=1.0f;
getWindow().setAttributes(lp);
}
});
}
}
注意事项
注意,在res文件夹下创建menu文件夹,然后创建使用的menu文件
Notifcation通知
普通通知
private void sendNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建通知,t通过builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置属性
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("没有作业真好");//题目
builder.setContentText("不写作业,挑战极限");//正文
builder.setContentInfo("放假真好");//附加消息
builder.setAutoCancel(true);//点击自动关闭
//大图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
//添加点击事件
//打开activity,使用intent
Intent intent = new Intent();
intent.setClass(this,Main2Activity.class);
// 延时意图 只能执行一次
PendingIntent pIntent=PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG_ONE_SHOT);//专门跳转activity的方法
//延时意图设置个本身
builder.setContentIntent(pIntent);
Notification notification = builder.build();
notificationManager.notify(20,notification);
}
进度条通知
//带进度条的通知
private void sendNotification2(){
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
final Notification.Builder builder=new Notification.Builder(this);
//小图标
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("欢迎来到英雄联盟");
//正文部分为进度条
builder.setProgress(100,p,false);
notificationManager.notify(2,builder.build());
CountDownTimer count = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
p+=10;
builder.setProgress(100,p,false);
//重新发送一个新的通知
notificationManager.notify(2,builder.build());
}
@Override
public void onFinish() {
//关闭通知
notificationManager.cancel(2);
}
}.start();
}
自定义通知 跳转
private void sendNotification3() {
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
final Notification.Builder builder=new Notification.Builder(this);
//小图标
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
//自定义通知
builder.setContentTitle("自定义通知");
//系统提供通知yong viewgroup
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.item);
remoteViews.setTextViewText(R.id.tv2,"修改的内容");
//修改图片
remoteViews.setImageViewResource(R.id.iv2,R.mipmap.ic_launcher);
//添加点击事件
//打开activity,使用intent
Intent intent = new Intent();
intent.setClass(this,Main2Activity.class);
// 延时意图 只能执行一次
PendingIntent pIntent=PendingIntent.getActivity(this,10,intent,PendingIntent.FLAG_ONE_SHOT);//专门跳转activity的方法
remoteViews.setOnClickPendingIntent(R.id.bt22,pIntent);
builder.setContent(remoteViews);
notificationManager.notify(3,builder.build());
}
双击退出
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
long nowtime=0;
long firsttime=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e(TAG, "onKeyDown: " );
switch (keyCode){
case 4:
//判断
if ((System.currentTimeMillis()-firsttime)>1000){
firsttime=System.currentTimeMillis();
}else {
//退出
finish();
}
//获取系统时间
nowtime = System.currentTimeMillis();
break;
}
return false;
}
}