菜单与双击退出
普通菜单
在reg文件夹下创建menu文件夹
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/red" android:title="红色" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom"></item>
<item android:id="@+id/blue" android:title="蓝色"></item>
</menu>
加载菜单文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
```
==点击事件==
```java
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
break;
case R.id.blue:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
上下文菜单
在reg文件夹下创建menu文件夹
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/red" android:title="红色" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom"></item>
<item android:id="@+id/blue" android:title="蓝色"></item>
</menu>
加载菜单文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu, menu);
}
```
==点击事件==
```java
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
tv.setTextColor(Color.RED);//更换文字颜色
break;
case R.id.blue:
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
tv.setTextColor(Color.BLUE);
break;
}
return super.onContextItemSelected(item);
}
弹出窗口
public void bnt(View view) {
//弹出窗口
final PopupWindow popupWindow = new PopupWindow(this);
//布局
View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
//点击外部可取消
popupWindow.setOutsideTouchable(true);
//点击事件
final TextView textView1 = inflate.findViewById(R.id.pop_tv);
final TextView textView2 = inflate.findViewById(R.id.pop_tv2);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView1.getText(), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
//点击事件
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView2.getText(), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
//弹出窗口聚焦显示 改变外部透明度
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.5f;
getWindow().setAttributes(attributes);
//消失监听器
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=1f;
getWindow().setAttributes(attributes);
}
});
popupWindow.setAnimationStyle(R.style.pop);
popupWindow.setContentView(inflate);
//宽
popupWindow.setWidth(400);
//高
popupWindow.setHeight(300);
//显示在哪里
//popupWindow.showAsDropDown(tv,10,10);
//居中显示 还有BUTTOM和TOP属性
View inflate2 = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
popupWindow.showAtLocation(inflate2, Gravity.CENTER,0,0);
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/pop_tv"/>
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/pop_tv2"/>
</LinearLayout>
在onCreate方法中绑定控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
registerForContextMenu(tv);//与控件绑定
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建一个PopupMenu对象
PopupMenu popupMenu = new PopupMenu(MainActivity.this, tv);
//加载布局
popupMenu.inflate(R.menu.menu);
//点击事件
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.red:
tv.setTextColor(Color.RED);//更换文字颜色
break;
case R.id.blue:
tv.setTextColor(Color.BLUE);
break;
}
return false;
}
});
popupMenu.show();
}
});
}
双击推出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//如果按得是回退键就提示用户
if (keyCode==KeyEvent.KEYCODE_BACK){
//再次按下在两秒之内
if (System.currentTimeMillis()- time>2000){
Toast.makeText(this, "再按退出", Toast.LENGTH_SHORT).show();
time=System.currentTimeMillis();
return true;
}else {
finish();
}
}
return super.onKeyDown(keyCode, event);
}