点击按钮就可以了

时间:2015-01-21 08:29:40

标签: java android xml eclipse copy

如何点击按钮(电话号码)并将其复制并显示已复制?

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");


    }
}

我这样做了,数字没有被复制,只有一个msg出现在toast中..我只是想点击按钮,它告诉我这个数字被复制了。当我去拨号器时,它什么都没有粘贴..帮助:)

2 个答案:

答案 0 :(得分:2)

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

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

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

获得

答案 1 :(得分:1)

公共类PageOneHotel扩展了活动{

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();
        }
    });


}

}