Meun的继承关系
Menu的继承关系.png
ContextMenu上下文菜单
在res文件夹中创建menu菜单
在res/menu中创建菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--条目组-->
<group>
<!--每个条目-->
<item
android:id="@+id/item1"
android:title="选项1" />
<item
android:id="@+id/item2"
android:title="选项2" />
<item
android:id="@+id/item3"
android:title="选项3" />
</group>
</menu>
主类中编写代码
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> datas = new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
listView = findViewById(R.id.listView);
//假数据
for (int i = 0; i < 20; i++) {
datas.add("叽里咕噜前来报到 --->"+i);
}
//创建适配器
adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,datas);
//创建适配器
listView.setAdapter(adapter);
//给listView注册上下文菜单
registerForContextMenu(listView);
}
//重写创建上下文菜单的方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//设置菜单样式
getMenuInflater().inflate(R.menu.context_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//上下文菜单的监听
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
return super.onContextItemSelected(item);
}
}
OptionMenu 选项菜单
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//重写onCreateOptionsMenu方法创建系统菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/****
* groupId 组ID
* itemId 条目ID
* order 条目顺序(-1000 >= order >= 1000)
* title 标题 即要显示的位置
*/
//添加条目
menu.add(2, 3, 1, "风继续吹");
menu.add(2, 4, 2, "春夏秋冬");
menu.add(2, 5, 3, "无心睡眠");
menu.add(2, 6, 4, "有谁共鸣");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
return true;
}
}
va代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//重写onCreateOptionsMenu方法创建系统菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/****
* groupId 组ID
* itemId 条目ID
* order 条目顺序(-1000 >= order >= 1000)
* title 标题 即要显示的位置
*/
//添加子菜单
SubMenu subMenuGuo = menu.addSubMenu(1, 1, 1, "张国荣");
subMenuGuo.add(2, 3, 1, "风继续吹");
subMenuGuo.add(2, 4, 2, "春夏秋冬");
subMenuGuo.add(2, 5, 3, "无心睡眠");
subMenuGuo.add(2, 6, 4, "有谁共鸣");
//给子菜单添加条目
SubMenu subMenuXue = menu.addSubMenu(2, 2, 2, "张学友");
subMenuXue.add(3, 7, 1, "饿狼传说");
subMenuXue.add(3, 8, 2, "吻别");
subMenuXue.add(3, 9, 3, "李香兰");
subMenuXue.add(3, 10, 4, "旧情绵绵");
return true;
}
//选择条目的监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
}
PopupWindow
PopupWindow的继承关系
PopupWindow的继承关系.png
PopupWindow实现
编写PopupWindow要显示的布局
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="=密码"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
实例化PopupWindow 设置属性并显示
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button eject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eject = findViewById(R.id.eject);
}
public void eject(View view) {
//实例化对象
final PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//获得要显示的视图
View showView = MainActivity.this.getLayoutInflater().inflate(R.layout.login, null);
//设置视图
popupWindow.setContentView(showView);
//设置窗口的高
popupWindow.setHeight(400);
//设置窗口的宽
popupWindow.setWidth(600);
//将窗口外部点击消失
popupWindow.setOutsideTouchable(true);
//设置获得焦点
popupWindow.setFocusable(true);
//将窗口显示在eject控件之下
// popupWindow.showAsDropDown(eject,200,200);
//将窗口显示在父控件的指定位置
popupWindow.showAtLocation(showView, Gravity.CENTER,0,0);
//找到控件
final EditText userName = showView.findViewById(R.id.userName);
final EditText password = showView.findViewById(R.id.password);
Button login = showView.findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "用户名:"+userName.getText().toString()+",密码:"+password.getText().toString(), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
WindowManager.LayoutParams attributes = getWindow().getAttributes();
//设置透明度
attributes.alpha = 0.3f;
//设置给Activity
getWindow().setAttributes(attributes);
//关闭PopupWindow的监听
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
//设置透明度
attributes.alpha = 1.0f;
//设置给Activity
getWindow().setAttributes(attributes);
}
});
}
}