0

我的相对布局中有 imageview、textview 和其他视图。现在我的问题是 textview 颜色在聚焦时没有改变。我的布局是

<RelativeLayout android:id="@+id/view"
 android:background="@drawable/bg1"
 android:focusable="true"
 android:clickable="true" 
android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 <TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/text" 
android:textStyle="bold" 
android:textSize="20dp"
 android:textColor="@color/selector" /> 

我在这里提到并添加android:duplicateParentState="true"到我的文本视图中,但这个解决方案也不适用于我..帮助我解决这个问题..提前谢谢..

编辑:如果我使用 android:background 我得到一个错误,我的 logcat 是

    11-12 08:39:20.916: E/AndroidRuntime(4691): FATAL EXCEPTION: main
11-12 08:39:20.916: E/AndroidRuntime(4691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myview.myapp/com.myview.myapp.Mainclass}: android.view.InflateException: Binary XML file line #43: Error inflating class <unknown>
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.os.Looper.loop(Looper.java:137)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at android.app.ActivityThread.main(ActivityThread.java:4429)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-12 08:39:20.916: E/AndroidRuntime(4691):     at dalvik.system.NativeStart.main(Native Method)
11-12 08:39:20.916: E/AndroidRuntime(4691): Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class <unknown>

这里第 43 行是 xml 中 textview 的开始

4

3 回答 3

1

嗨,我刚刚尝试了以下方式,它对我有用

创建一个可绘制文件 colorchange.xml 如下,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@android:color/holo_blue_dark"/>
    <item android:state_pressed="true" android:color="@android:color/holo_red_light" />
    <item android:state_focused="false" android:color="@android:color/white" />
    <item android:state_pressed="false" android:color="@android:color/white" /> 
</selector>

然后将xml添加到textview的textcolor中,

android:textColor="@drawable/colorchange"

希望这可以帮助。

于 2015-09-11T14:13:21.840 回答
0

为您的 TextView 实现 OnFocusChangeListener

textView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
        //do something
    }else {
       //do something 
    }
   }
});
于 2013-11-12T07:38:52.373 回答
0

实现 XML Drawable。您可以通过在 xml 中定义来实现 onFocus、onPress 等效果。我的自定义.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
<item android:drawable="@drawable/image_name_while_notpressed" />  //means normal
</selector>

Android 中的 textView xml。

<TextView
.
.
.
.
android:Background="@drawable/mycustom.xml"
.
/>
于 2013-11-12T07:42:56.590 回答