2011-12-23 22 views
4

電卓の回答をクリップボードにコピーし、電卓を閉じて別のウィンドウを開くコードを書いた。答えはコードを使用してここに貼り付けてください:Androidのクリップボードから貼り付け

textOut2= (TextView) findViewById(R.id.etInput1); 
    final ClipboardManager clipBoard= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
    textOut2.setText(clipBoard.getText()); 

これは決して動作しません。間違いかもしれませんか? p.s.私は長いテキストを使用して貼り付けることができるので、どのテキストがコピーされるのか知っていますが、私はそれを自動で行いたいと思います。コピーされたテキストの特定の名前を付けることは可能ですか?私は別のTextViewの

答えて

10

公共のCharSequenceのgetText() を試してみてください:このメソッドは推奨されませんAPIレベル11 。 代わりにgetPrimaryClip()を使用してください。これにより、プライマリクリップが取得され、ストリングに変換されます。

String textToPaste = null; 

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 

if (clipboard.hasPrimaryClip()) { 
    ClipData clip = clipboard.getPrimaryClip(); 

    // if you need text data only, use: 
    if (clip.getDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) 
     // WARNING: The item could cantain URI that points to the text data. 
     // In this case the getText() returns null and this code fails! 
     textToPaste = clip.getItemAt(0).getText().toString(); 

    // or you may coerce the data to the text representation: 
    textToPaste = clip.getItemAt(0).coerceToText(this).toString(); 
} 

if (!TextUtils.isEmpty(textToPaste)) 
    ((TextView)findViewById(R.id.etInput1)).setText(textToPaste); 

あなたはClipData.addItem()を経由して、テキストを追加ClipData.Item項目を追加することが許可されているが、それらを識別する方法はありません。

2

の多くを持っているとして、それが簡単に単語を貼り付けることになるだろうとしているので、この

textOut2= (TextView) findViewById(R.id.etInput1); 
final ClipboardManager clipBoard= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
String temp = new String; 
temp = clipBoard.getText().toString(); 
textOut2.setText(temp); 
関連する問題