Android 在 TextView 或 EditView 中选择一个单词

发布于 2024-10-04 20:53:02 字数 169 浏览 2 评论 0原文

我试图找出一种简单的方法让用户选择一个单词,最好是长按 TextView 中的单词。基本上,我有一个充满文本的 TextView,我希望用户能够长按单词,然后显示上下文菜单,以便我可以执行数据库搜索?这可能吗?我也可以切换到 EditText,只要我能让它看起来像 TextView 即可。有道理吗?

谢谢。

I am trying to figure out an easy way for a user to select a word, preferably by long pressing on the word in a TextView. Basically, I have a TextView filled with text and I would like the user to have the ability to long press the word and then display a contextmenu so I can execute a database search? Is this possible? I can also switch to an EditText as long as I can make it look like a TextView. Make sense?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-10-11 20:53:02

很简单。

首先创建您的 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>

Very simple.

First create your TextView and 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);
}

Then build your 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文