2017-09-07 3 views
0

私は背景画像を変更したい2つのテキストビューを持っています。はデフォルトでボタンに状態を設定します

<TextView 
      android:layout_width="@dimen/margin_0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:layout_margin="@dimen/margin_padding_2dp" 
      android:padding="@dimen/margin_padding_5dp" 
      android:text="Trainings" 
      android:gravity="center" 
      android:textSize="17sp" 
      android:background="@drawable/background_selector" 
      android:clickable="true" 
      /> 
     <TextView 
      android:id="@+id/learning_programs" 
      android:layout_width="@dimen/margin_0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="Learning Programs" 
      android:padding="@dimen/margin_padding_5dp" 
      android:textSize="@dimen/text_size_seventeen" 
      android:background="@drawable/background_selector" 
      android:gravity="center" 
      android:clickable="true" 
      /> 

ですから、私が望むのは、TextViewをクリックすると、背景イメージが変わるはずです。そのため、私は

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

    <item android:drawable="@drawable/rounded_corner_grey"/> 
</selector> 

ように描画可能でセレクタを有するが、上記のコードの問題は、ユーザーがのTextViewをタップする場合にのみ白色を保持することです。ユーザーの指が動くと、色はグレーに変わります。

私が望むのは、ユーザーがTextViewを一度クリックした後に、色が白になることです。

state_selected:trueの場合、両方のTextViewsは白くなり、クリックできません。

答えて

1

このコード行を両方のテキストビューに追加します。

アンドロイド:focusableInTouchMode =「true」を

これは、携帯電話は、タッチモードのときにビューがフォーカスを得ることができることを意味します。

EDIT: このXMLを試して、機能するかどうかを確認してください。コードには、その後、

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

:私の場合は

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="@dimen/margin_0dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/margin_padding_2dp" 
    android:layout_weight="0.5" 
    android:background="@drawable/background_selector" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:gravity="center" 
    android:padding="@dimen/margin_padding_5dp" 
    android:text="Trainings" 
    android:textSize="17sp" /> 

<TextView 
    android:id="@+id/learning_programs" 
    android:layout_width="@dimen/margin_0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/background_selector" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:gravity="center" 
    android:padding="@dimen/margin_padding_5dp" 
    android:text="Learning Programs" 
    android:textSize="@dimen/text_size_seventeen" /> 

+0

のための1 OnClickListenerを使用することができます! – Aayushi

+0

それはうまくいくはずです。あなたのコードを私のスタジオにコピーして、仲間をチェックしました。 – Anonymous

+0

いいえ、そうではありません。私も同じことを試みています。 – Aayushi

0

、私はこれを使用

がビューからあなたの指のリリースが、あなたのビューが
TextView tv = (TextView)findViewById(R.id.tv); 
tv.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //TODO 
      tv.setEnable(false); 
     } 
    } 
+0

私は2つのtextViewsを持っていますので、一般化されたコードが必要です – Aayushi

+0

私はカスタムビューをコードする必要がありますより効果的だと思います –

1

ときselectedまたはpressedの状態ではありません。クリックイベント後に手動でpressedまたはselectedの状態を設定する必要があります。

<TextView 
    android:id="@+id/sample_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:background="@drawable/background_selector" 
    android:text="Hello World!" /> 

は、次に、あなたのコード

TextView tv = (TextView) findViewById(R.id.sample_text); 
tv.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     v.setSelected(!v.isSelected()); 
    } 
}); 

にあなたはまだ動作しません2 TextView

private View.OnClickListener toggleSelectedStateOnClickListener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     v.setSelected(!v.isSelected()); 
    } 
}; 

TextView tv = (TextView) findViewById(R.id.sample_text); 
tv.setOnClickListener(toggleSelectedStateOnClickListener); 

TextView tv1 = new TextView(this); 
tv1.setOnClickListener(toggleSelectedStateOnClickListener); 
+0

私は2つのTextViewsを持っています。 – Aayushi

+0

@Aayushi編集された答えを参照してください。 – alijandro

+0

このコードでは、一度に両方のtextViewを選択できます。一度に1つずつ選択する方法はありますか?つまり、他の方法を選択すると、最初のものが自動的に選択解除されます。 – Aayushi

関連する問題