Monday, May 21, 2012 : Article User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
15
1.สร้างโปรเจค Android ใช้ชื่อว่า AndroidCopyPaste


2.เปิด file main.xml


ใส่โค้ดดังนี้


<?
xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:orientation="vertical"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 >

<TextView

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 android:text="@string/hello"

 />

<EditText

 android:id="@+id/src"

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 />

<TextView

 android:id="@+id/dest"

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 />

<Button

 android:id="@+id/copynpaste"

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 android:text="Copy'n Paste"

 />

</LinearLayout>


ผลลัพธ์ที่ได้


3.เปิด file AndroidCopyPaste.java


จากนั้นให้ใส่โค้ดดังนี้

package th.co.bighead.exsample.AndroidCopyPaste;

 

import android.app.Activity;

import android.os.Bundle;

import android.text.ClipboardManager;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class AndroidCopyPaste extends Activity {

 /** Called when the activity is first created. */

 @Override

 public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);

 

     final EditText src = (EditText)findViewById(R.id.src);

     final TextView dest = (TextView)findViewById(R.id.dest);

     Button copynPaste = (Button)findViewById(R.id.copynpaste);

 

     final ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

 

     copynPaste.setOnClickListener(new Button.OnClickListener(){

 

   @Override

   public void onClick(View v) {

    // TODO Auto-generated method stub

    clipBoard.setText(src.getText());

    dest.setText(clipBoard.getText());

   

   }});

 }

}


4.รันโปรแกรม จะได้ผลการรันดังรูปภาพ

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi