如何通过在Android中单击按钮将文本从EditText复制到另一个EditText? [英] How To Copy a Text from a EditText to another EditText by button clicking in Android?

查看:138
本文介绍了如何通过在Android中单击按钮将文本从EditText复制到另一个EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序,当您单击按钮时,将您在EditText中输入的文本复制到其他EditText ...

i wanted to write an app that when you click on the button , copy the text you entered in the EditText to other EditText...

我写了以下代码:

package com.example.learningapp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

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

    Button Copy = (Button) findViewById(R.id.button1);
    Copy.setOnClickListener(this);
}


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


@Override
public void onClick(View v){
    enteredText.getText().toString();
    copiedText.setText((CharSequence) enteredText);  
}
}

这是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="com.example.learningapp.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="54dp"
    android:ems="10" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="149dp"
    android:ems="10" />

<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="57dp"
    android:onClick="onClick"
    android:text="Copy" />

 </RelativeLayout>

所以这不会运行.我收到很多错误,我不知道它们是什么.所以有人帮助我吗?

so this wont run. i get lots of errors i dont know what they are. so somebody help me with it ??

这是logcat:

08-16 02:58:43.929: E/AndroidRuntime(1860): FATAL EXCEPTION: main
08-16 02:58:43.929: E/AndroidRuntime(1860): Process: com.example.learningapp, PID: 1860   
-16 02:58:43.929: E/AndroidRuntime(1860): java.lang.RuntimeException: Unable to instantiate    activity ComponentInfo{com.example.learningapp/com.example.learningapp.MainActivity}: java.lang.NullPointerException
08-16 02:58:43.929: E/AndroidRuntime(1860):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.os.Looper.loop(Looper.java:136)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invoke(Method.java:515)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at dalvik.system.NativeStart.main(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860): Caused by: java.lang.NullPointerException
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.Activity.findViewById(Activity.java:1884)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.example.learningapp.MainActivity.<init>(MainActivity.java:22)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.Class.newInstanceImpl(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.Class.newInstance(Class.java:1208)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)

推荐答案

我认为这是问题所在

public void onClick(View v){
    enteredText.getText().toString();
    copiedText.setText((CharSequence) enteredText);  
}

替换为

public void onClick(View v){

    copiedText.setText(enteredText.getText().toString(););  
}

从您的LOGCAT登录

可以看出,您已经将这两行放在了

It can be seen that you have put these two lines

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

onCreate()之外.这肯定是一个错误,因为必须初始化的任何视图都必须在onCreate()

outside onCreate().That'll surely be an error as any view that has to be initialised has to be done in onCreate()

因此,请将这两行移至您的onCreate()

So,Kindly move those two lines into your onCreate()

这篇关于如何通过在Android中单击按钮将文本从EditText复制到另一个EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆