0

相対レイアウト内にイメージビュー、テキストビュー、およびその他のビューがあります。今私の問題は、フォーカスされているときにテキストビューの色が変更されないことです。私のレイアウトは

<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のテキストビューの始まりです

4

3 に答える 3

0

XML Drawable を実装します。onFocus、onPressなどのエフェクトをxmlで定義することで実装できます。mycustom.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 に答える
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 に答える