简体   繁体   中英

Adding a new TextView into a layout from an EditText and Button

The app I am working on has an EditText at the bottom (along with a button). When that button is pressed, I am trying to have the EditText converted into a TextView and placed inside a linear layout. How can I go about this? Each example I've looked at hasn't worked for me.

I've tried this code:

public class Notes extends Fragment {

    LinearLayout llNotes;
    Button butNotes;
    EditText etNotes;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.notes, container, false);

        return rootView;


    }

    public void sendText(View view){
        etNotes=(EditText)findViewById(R.id.etNotes);
        String message=etNotes.getText().toString();
        llNotes=(LinearLayout)findViewById(R.id.llNotes);
        TextView text = new TextView(this);
        text.setText(message);
        llNotes.addView(text);

    }
}

The linear layout is inside of a scrollview inside of a frame layout. For the above code, I can't even type in the sendText part without it giving me an error (the sendText is an android:OnClick). Sorry I'm new to Android.

At the moment, in the "sendText" method, all the findViewById are errored as well as "new TextView(this)"


LOGCAT

05-11 15:23:35.713: D/ActivityThread(3750): setTargetHeapUtilization:0.75
05-11 15:23:35.713: D/ActivityThread(3750): setTargetHeapIdealFree:2097152
05-11 15:23:35.933: E/SensorManager(3750): thread start
05-11 15:23:35.933: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:35.953: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fdc,f(
05-11 15:23:35.953: D/SensorManager(3750): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:35.953: I/SensorManager(3750): Set min delay = 200000
05-11 15:23:35.953: I/SensorManager(3750): Before sending... delay = 200000
05-11 15:23:35.953: D/Sensors(3750): Remain listener = android.view.OrientationEventListener$SensorEventListenerImpl@41edc4e0
05-11 15:23:35.953: I/Sensors(3750): sendDelay --- 200000000
05-11 15:23:35.953: D/SensorManager(3750): JNI - sendDelay
05-11 15:23:35.993: D/libEGL(3750): loaded /system/lib/egl/libEGL_adreno200.so
05-11 15:23:36.013: D/libEGL(3750): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-11 15:23:36.013: D/libEGL(3750): loaded /system/lib/egl/libGLESv2_adreno200.so
05-11 15:23:36.083: I/Adreno200-EGLSUB(3750): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-11 15:23:36.103: E/(3750): <s3dReadConfigFile:75>: Can't open file for reading
05-11 15:23:36.103: E/(3750): <s3dReadConfigFile:75>: Can't open file for reading
05-11 15:23:36.103: D/OpenGLRenderer(3750): Enabling debug mode 0
05-11 15:23:36.233: D/SensorManager(3750): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fdc,f(
05-11 15:23:36.233: D/Sensors(3750): Remain listener = Sending .. normal delay 200ms
05-11 15:23:36.233: I/Sensors(3750): sendDelay --- 200000000
05-11 15:23:36.243: D/SensorManager(3750): JNI - sendDelay
05-11 15:23:36.243: I/SensorManager(3750): Set normal delay = true
05-11 15:23:36.243: D/SensorManager(3750): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*fd..:8
05-11 15:23:36.293: W/InputMethodManager(3750): Ignoring onBind: cur seq=1819, given seq=1818
05-11 15:23:36.313: W/IInputConnectionWrapper(3750): getExtractedText on inactive InputConnection
05-11 15:23:36.333: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.333: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.484: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.484: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): getExtractedText on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.514: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:36.604: W/IInputConnectionWrapper(3750): beginBatchEdit on inactive InputConnection
05-11 15:23:36.604: W/IInputConnectionWrapper(3750): endBatchEdit on inactive InputConnection
05-11 15:23:51.078: D/AndroidRuntime(3750): Shutting down VM
05-11 15:23:51.078: W/dalvikvm(3750): threadid=1: thread exiting with uncaught exception (group=0x4145c360)
05-11 15:23:51.088: E/AndroidRuntime(3750): FATAL EXCEPTION: main
05-11 15:23:51.088: E/AndroidRuntime(3750): java.lang.IllegalStateException: Could not find a method sendText(View) in the activity class net.tagyoureit.heykeepup.MainActivity for onClick handler on view class android.widget.Button with id 'butNotes'
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$1.onClick(View.java:3677)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View.performClick(View.java:4192)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$PerformClick.run(View.java:17254)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Handler.handleCallback(Handler.java:615)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.os.Looper.loop(Looper.java:137)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.app.ActivityThread.main(ActivityThread.java:4950)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at dalvik.system.NativeStart.main(Native Method)
05-11 15:23:51.088: E/AndroidRuntime(3750): Caused by: java.lang.NoSuchMethodException: sendText [class android.view.View]
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at java.lang.Class.getMethod(Class.java:915)
05-11 15:23:51.088: E/AndroidRuntime(3750):     at android.view.View$1.onClick(View.java:3670)
05-11 15:23:51.088: E/AndroidRuntime(3750):     ... 11 more

"findViewById" doesn't exist in Fragment...
You should use rootView.findViewById(...) to get your target view.
Also, the android:onClick attribute in xml file will find the same function name in Activity but Fragment.

So you should define your onClick function in Activity.
But if you want define the onClick function in Fragment, remove the android:onClick attribute in xml file, and use Button.setOnClickListener(OnclickListener) function to setup the OnClickListener of Button in Fragment.

Here is some example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.notes, container, false);
    llNotes=(LinearLayout)rootView.findViewById(R.id.llNotes);
    etNots=(EditText)rootView.findViewById(R.id.etNotes);

    etNotes.setOnClickListener(notesListener);

    return rootView;
}

private OnClickListener notesListener = new OnClickListener() {
    public void onClick(View view) {
        String message=etNotes.getText().toString();

        TextView text = new TextView(getActivity());
        text.setText(message);
        llNotes.addView(text);
    }
};

And remove the 'android:onClick="sendText"' in your layout xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM