单击按钮,这样它就变成了科佩德

Click button so it would be coppeid

我怎样才能点击一个按钮(phone 号)并复制它并告诉我它已被复制?

activity_main.xml

<Button
android:id="@+id/phonenumberhotel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Tel.: 00000 0 000 0000"/>

MainActivity.java

package com.iss.dfdfd;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class PageOneHotel extends Activity {


    Button phonenumberhotel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotel);
        Toast.makeText(getBaseContext(), "Phone number has been copied", Toast.LENGTH_LONG).show();
        addButtonListener();
        addListenerOnButton();

    }


    public void addListenerOnButton() {

        phonenumberhotel = (Button) findViewById(R.id.phonenumberhotel);

        phonenumberhotel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO add action here
            }
        });
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        ClipData clip = ClipData.newPlainText("label", "Copied");


    }
}

我做到了,号码没有被复制,吐司中只出现一条消息..我只想点击按钮,它告诉我号码已被复制。当我转到拨号器时,它什么也没粘贴..帮助:)

这里是在 Android 中将文本复制到剪贴板的代码片段:How to copy text programmatically in my Android app?

在创建您的按钮时添加 .setOnClickListener() 您自己的侦听器,该侦听器在他的方法 onClick() 中调用给定的代码片段。

Button的文本可以通过button.getText().toString()

获取

public class PageOneHotel 扩展 Activity {

final Button phonenumberhotel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hotel);
    addButtonListener();
    addListenerOnButton();

}


public void addListenerOnButton() {

    phonenumberhotel = (Button) findViewById(R.id.phonenumberhotel);

    phonenumberhotel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
             String number = phonenumberhotel.getText().toString()
             ClipData clip = ClipData.newPlainText("Phone number",number);   
             clipboard.setPrimaryClip(clip);
             Toast.makeText(getBaseContext(), "Phone number has been copied", Toast.LENGTH_LONG).show();
        }
    });


}

}