android 长按事件 和 长按弹出上下文菜单如何处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 长按事件 和 长按弹出上下文菜单如何处理相关的知识,希望对你有一定的参考价值。
一个按钮 既定义了长按弹出上下文菜单,又设置了长按监听,当长按该按钮时触发那个事件
参考技术A android 底层在当你点击控件2秒后就属于长按事件了,则进入长按事件监听,当点击少于2秒就直接进入点击事件了,望有帮助本回答被提问者采纳 参考技术B 你可以在监听里设置打印啊,看那个先出来就是哪个~Android之弹出菜单框注冊上下文菜单
注冊上下文菜单:(长按弹出一个菜单)
第一种创建方法(与长按事件结合):
public class MainActivity extends Activity {
private TextView username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
username.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add("file");
menu.add("edit");
menu.add("source");
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//当菜单上下文元素被选中时运行
Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
return super.onContextItemSelected(item);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//菜单元素被选中时运行
Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
return super.onMenuItemSelected(featureId, item);
}
}
另外一种方法:
(注冊上下文菜单)
public class MainActivity extends Activity {
private TextView username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
this.registerForContextMenu(username);//注冊菜单
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add("file");
menu.add("edit");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//当菜单上下文元素被选中时运行
Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
return super.onContextItemSelected(item);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//菜单元素被选中时运行
Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
return super.onMenuItemSelected(featureId, item);
}
}
第一种创建方法(与长按事件结合):
public class MainActivity extends Activity {
private TextView username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
username.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add("file");
menu.add("edit");
menu.add("source");
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//当菜单上下文元素被选中时运行
Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
return super.onContextItemSelected(item);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//菜单元素被选中时运行
Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
return super.onMenuItemSelected(featureId, item);
}
}
另外一种方法:
(注冊上下文菜单)
public class MainActivity extends Activity {
private TextView username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
this.registerForContextMenu(username);//注冊菜单
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add("file");
menu.add("edit");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//当菜单上下文元素被选中时运行
Toast.makeText(this, item.getTitle()+"onContextItemSelected", 1).show();
return super.onContextItemSelected(item);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
//菜单元素被选中时运行
Toast.makeText(this, item.getTitle()+"onMenuItemSelected", 1).show();
return super.onMenuItemSelected(featureId, item);
}
}
以上是关于android 长按事件 和 长按弹出上下文菜单如何处理的主要内容,如果未能解决你的问题,请参考以下文章
android中RecyclerView控件实现长按弹出PopupMenu菜单功能