2010-11-30 7 views
3

私は、ユーザーが単語を選択する簡単な方法を見つけようとしています。TextViewの単語を長く押してください。基本的には、TextViewにテキストが埋め込まれているので、単語を長押ししてコンテキストメニューを表示してデータベース検索を実行できるようにしたいと思いますか?これは可能ですか? TextViewのように見える限り、EditTextに切り替えることもできます。理にかなっている?AndroidはTextViewまたはEditViewのいずれかで単語を選択します

ありがとうございました。

答えて

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> 
+0

。私は誰にも電話したくない。私は彼らが長い間押された場所の下のテキストをコピーしたい。 –

+0

それをコピーするか、長い間押されたテキストにアクセスするだけです。 –

+0

私は、コンテキストメニューでTextViewの例を挙げました。あなたが望むものではありませんか? – j3ffz

関連する問題