我试图找出一种简单的方法让用户选择一个单词,最好是长按TextView中的单词。基本上,我有一个文本填充文本,我希望用户能够长按该单词,然后显示上下文菜单,以便我可以执行数据库搜索?这可能吗?我也可以切换到EditText,只要我可以使它看起来像TextView。有意义吗?
感谢。
答案 0 :(得分:0)
很简单。
首先创建TextView和registerForContextMenu():
private AdapterContextMenuInfo info;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.txtbtn);
text.setText("Click Me!");
registerForContextMenu(text);
}
然后构建您的ContextMenu:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterView.AdapterContextMenuInfo)menuInfo;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
String phone="555-555-555";
String toDial="tel:"+phone.toString();
Uri uri = Uri.parse(toDial);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
return true;
default:
return super.onContextItemSelected(item);
}
}
<强> context_menu.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/call"
android:title="CALL" />
</menu>