How To Copy a Text from a EditText to another EditText by button clicking in Android?

无人久伴 提交于 2020-04-22 07:46:49

问题


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

i wrote this codes:

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

and this is the XML file

<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 ??

and this is 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)

回答1:


I think this is the problem

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

Replace it as

public void onClick(View v){

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

FROM your 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);

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

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




回答2:


Replace your onClick with the following

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



回答3:


Your code looks ok (for the most part). My guess is that you get a ClassCastException because you try to set the first edit text (which is not a CharSequence, but a View) as the text of the second EditText.

You have to change your onClick method to something like:

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

Also a minor style improvement would be to start variables (such as Copy) with a lower case letter.

Edit: I just saw the your Edit Text initialisation code is outside the onCreate method, which (if not a typo), probably also results in an error




回答4:


Try this

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 implements OnClickListener {
EditText enteredText;
EditText copiedText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button Copy = (Button) findViewById(R.id.button1);
   // here is the change you have to do remove edittext from both enteredText , copied text just from here
    enteredText = (EditText) findViewById(R.id.editText1);
    copiedText = (EditText) findViewById(R.id.editText2);
    Copy.setOnClickListener(this);
}





@Override
public void onClick(View v){

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

enter image description here




回答5:


Just do it :

copiedText.setText(enteredText.getText());



回答6:


Try with this:

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


来源:https://stackoverflow.com/questions/25331579/how-to-copy-a-text-from-a-edittext-to-another-edittext-by-button-clicking-in-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!