将文本字段数据复制到其他文本字段Android

时间:2013-07-23 09:03:54

标签: android events onclick copy

我需要将文本字段数据复制到另一个onclick 下面是我的活动主页,我的应用程序的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:ems="10"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:text="Button" />

    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="108dp"
        android:text="Chronometer" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="textPersonName" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

            EditText editText1 = (EditText) findViewById(R.id.editText1);
            EditText editText2 = (EditText) findViewById(R.id.editText2);

            Button b= (Button) findViewById(R.id.button1);

            b.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    String data = editText1.getText().toString();

                    editText2.setText(data);

            });

希望有所帮助

答案 1 :(得分:0)

package com.example.rohitsapp;

import android.os.Bundle;
import android.app.Activity;       
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editText1 = (EditText) findViewById(R.id.editText1);
    final EditText editText2 = (EditText) findViewById(R.id.editText2);

    Button b= (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            String data = editText1.getText().toString();

            editText2.setText(data);
        }

    });

    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

     getMenuInflater().inflate(R.menu.main, menu);
     return true;
 }

}

相关问题