我正在处理的应用程序底部有一个EditText(以及一个按钮)。当按下该按钮时,我试图将EditText转换为TextView并放置在线性布局中。我怎么能这样做?我看过的每个例子都不适用于我。
我试过这段代码:
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);
}
}
线性布局位于框架布局内部的滚动视图内部。对于上面的代码,我甚至无法输入sendText部分而没有给我一个错误(sendText是一个android:OnClick)。对不起,我是Android新手。
目前,在“sendText”方法中,所有findViewById都是错误的,以及“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
答案 0 :(得分:0)
所以你应该在Activity中定义你的onClick函数
但是如果你想在Fragment中定义onClick函数,删除xml文件中的android:onClick属性,并使用Button.setOnClickListener(OnclickListener)函数在Fragment中设置Button的OnClickListener。
以下是一些例子:
@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);
}
};
并删除布局xml文件中的'android:onClick =“sendText”。