我是Android新手。我想点击EditText
使ImageView可见public void clickHandler(View v) {
if (v.getId() == R.id.orderQty){
System.out.println(" edit text click");
System.out.println(" edit text id dilshad : "+v.getId());
int id = v.getId();
id = R.id.save_on_id;
System.out.println("save button id on button click : "+id);
int rew = id;
v.setId(id);
System.out.println("save button set and get id : "+ v.getId());
ImageView imageView = (ImageView)v;
imageView.setImageResource(R.drawable.save_on);
imageView.setVisibility(ImageView.VISIBLE);
}
}
这是我的Edittext
的xml<EditText
android:id="@+id/orderQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="false"
android:onClick="clickHandler" >
</EditText>
和这个ImageView在同一个xml中
<ImageView
android:id="@+id/save_on_id"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/separator_bottom"
android:background="#fff"
android:clickable="true"
android:visibility="gone"
android:onClick="clickHandler"
android:src="@drawable/save_on" />
但我得到了例外,
02-05 16:01:51.969: W/dalvikvm(10554): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-05 16:01:51.989: E/AndroidRuntime(10554): FATAL EXCEPTION: main
02-05 16:01:51.989: E/AndroidRuntime(10554): java.lang.IllegalStateException: Could not execute method of the activity
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.view.View$1.onClick(View.java:2072)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.view.View.performClick(View.java:2408)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.view.View$PerformClick.run(View.java:8816)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.os.Handler.handleCallback(Handler.java:587)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.os.Looper.loop(Looper.java:123)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-05 16:01:51.989: E/AndroidRuntime(10554): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:01:51.989: E/AndroidRuntime(10554): at java.lang.reflect.Method.invoke(Method.java:521)
02-05 16:01:51.989: E/AndroidRuntime(10554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-05 16:01:51.989: E/AndroidRuntime(10554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-05 16:01:51.989: E/AndroidRuntime(10554): at dalvik.system.NativeStart.main(Native Method)
02-05 16:01:51.989: E/AndroidRuntime(10554): Caused by: java.lang.reflect.InvocationTargetException
02-05 16:01:51.989: E/AndroidRuntime(10554): at com.example.demoekot.MainScreen.clickHandler(MainScreen.java:491)
02-05 16:01:51.989: E/AndroidRuntime(10554): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:01:51.989: E/AndroidRuntime(10554): at java.lang.reflect.Method.invoke(Method.java:521)
02-05 16:01:51.989: E/AndroidRuntime(10554): at android.view.View$1.onClick(View.java:2067)
02-05 16:01:51.989: E/AndroidRuntime(10554): ... 11 more
02-05 16:01:51.989: E/AndroidRuntime(10554): Caused by: java.lang.ClassCastException: android.widget.EditText
02-05 16:01:51.989: E/AndroidRuntime(10554): ... 15 more
02-05 16:01:55.189: I/Process(10554): Sending signal. PID: 10554 SIG: 9
有什么问题请帮我解决问题。在此先感谢所有人。
答案 0 :(得分:1)
您正试图将EditText
强制转换为ImageView
。
试试这个......
ImageView imageView = (ImageView) findViewById(R.id.save_on_id);
imageView.setImageResource(R.drawable.save_on);
imageView.setVisibility(ImageView.VISIBLE);
并且不要尝试更改View
的ID。 (您要将EditText
的ID分配给ImageView
)
修改了代码版本...试试这个
public void clickHandler(View v) {
if (v.getId() == R.id.orderQty) {
ImageView imageView = (ImageView) findViewById(R.id.save_on_id);
imageView.setImageResource(R.drawable.save_on);
imageView.setVisibility(ImageView.VISIBLE);
}
}
答案 1 :(得分:0)
非常复杂的代码。
if (v.getId() == R.id.orderQty) { // v is an instance of EditText
int id = v.getId(); // Useless initialisation
id = R.id.save_on_id;
int rew = id; // Useless
v.setId(id); // v is still an instance of EditText
ImageView imageView = (ImageView)v; // java.lang.ClassCastException: android.widget.EditText
//...
}
您最好使用findViewById方法
答案 2 :(得分:0)
试试这个..
public void clickHandler(View v) {
if (v.getId() == R.id.orderQty) {
System.out.println(" edit text click");
System.out.println(" edit text id dilshad : " + v.getId());
int id = v.getId();
id = R.id.save_on_id;
System.out.println("save button id on button click : " + id);
int rew = id;
v.setId(id);
System.out.println("save button set and get id : "
+ v.getId());
//hide edit text
v.setVisibility(View.GONE);
//show your image view
ImageView iv = (ImageView) findViewById(R.id.save_on_id);
iv.setImageResource(R.drawable.save_on);
iv.setVisibility(View.VISIBLE);
}
}
答案 3 :(得分:-3)
使用
View.VISIBLE
而不是
ImageView.VISIBLE
另外
如何点击view
<{1}}
那里存在一些逻辑问题。您点按的视图不是invisible
和ImageView
。所以在那个
EditText