textView上的选择器需要两次单击才能执行操作

时间:2017-06-22 11:45:43

标签: android android-xml android-drawable android-selector

我的textView上有选择线。

selector.xml:

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

我设置为textView的背景:

                   <TextView
                    android:id="@+id/displayPlace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:clickable="true"
                    android:text="@string/place"
                    android:textColor="@color/white"
                    android:background="@drawable/selector"
                    android:textSize="@dimen/text_main"/>

当我点击文字视图选择器时显示正常,但我必须对我的点击进行操作,所以为了做我的行动它需要点击两次,我试图删除android:focusableInTouchMode="true"行但是它让选择器显示了一点点然后消失了。

如何使用选择器首次点击操作?

我的点击:

place.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragment = new PlaceFragment();
            replaceFragment(fragment);
            Fragment fragment2 = getFragmentManager().findFragmentByTag(TAG_FRAGMENT);
            if (fragment2 != null)
                getFragmentManager().beginTransaction().remove(fragment2).commit();
        }
    });

2 个答案:

答案 0 :(得分:1)

尝试使用setOnFocusChangeListener代替setOnClickListener

答案 1 :(得分:0)

有两种方法可以使用双击

1)在OnClickListener中使用计时器https://gist.github.com/srix55/ec64d2f6a371c80bbbc4
2)使用GestureDetector onDoubleTap listnerer https://stackoverflow.com/a/13531025/4741746https://stackoverflow.com/a/21449185/4741746