티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to Copy Text to Clip Board in Android?

안드로이드에서 클립보드에 텍스트를 복사하는 방법은?

 문제 내용 

Can anybody please tell me how to copy the text present in a particular textview to clipboard when a button is pressed?

버튼을 눌렀을 때 특정 텍스트뷰에 있는 텍스트를 클립보드에 복사하는 방법을 알려주실 수 있나요?
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    textView = (TextView) findViewById(R.id.textview);
    copyText = (Button) findViewById(R.id.bCopy);
    copyText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            String getstring = textView.getText().toString();
            
            // Help to continue :)
        }
    });
}

 

I want to copy the Text in TextView textView to clipboard when the Button bCopy is pressed.

Button bCopy를 누르면 TextView textView의 텍스트를 클립보드에 복사하고 싶습니다.

 

 

 

 높은 점수를 받은 Solution 

use ClipboardManager

클립보드매니저 사용
 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText(label, text);
 clipboard.setPrimaryClip(clip);

 

make sure you have imported android.content.ClipboardManager and NOT android.text.ClipboardManager. Latter is deprecated. Check this link for Further information.

android.content.ClipboardManager를 임포트하세요. android.text.ClipboardManager는 아닙니다. 후자는 더 이상 사용되지 않습니다. 자세한 내용은 이 링크를 확인하십시오.

 

 

 

 가장 최근 달린 Solution 

If you want to copy text from edittext So First Create Edittext

에디특 텍스트에서 텍스트를 복사하려면 먼저 에디트 텍스트 만들기
    EditText mResultEt = findViewById(R.id.resultEt);

 

then Create One Button on which after clicking we can copy these text

그런 다음 클릭한 후 이 텍스트를 복사할 수 있는 단일 버튼 만들기
    ImageButton copyClipBoard = findViewById(R.id.btn_copy);

 

then use the listener of the button

그런 다음 버튼의 리스너를 사용합니다.

 

Java

자바
copyClipBoard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager clipboardManager = (ClipboardManager)
                        getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("nonsense_data",
                        mResultEt.getText().toString());
                clipboardManager.setPrimaryClip(clipData);
                Toast.makeText(MainActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
            }
        });

 

Kotlin

코틀린
btn1.setOnClickListener{
            val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText(
                "nonsense_data",
                content_et.getText().toString()
            )
            clipboardManager.setPrimaryClip(clipData)
            Toast.makeText(this@MainActivity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()

        }

 

and make sure to import this

그리고 이것을 꼭 임포트하세요.
import android.content.ClipboardManager;

 

don't import this one

이것은 임포트하면 안됩니다.
android.text.ClipboardManager

 

 

출처 : https://stackoverflow.com/questions/19253786/how-to-copy-text-to-clip-board-in-android

반응형
댓글
공지사항
최근에 올라온 글