【问题标题】:How to use OnTouch, OnFocus and OnClick?如何使用 OnTouch、OnFocus 和 OnClick?
【发布时间】:2021-12-31 23:21:59
【问题描述】:

我有一些TextViews,我第一次想在单击(聚焦)时更改背景颜色,第二次按下按钮应该将文本更改应用于聚焦的TextView

我完全不知道我需要做什么。

这是我如何更改背景颜色

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

这是我TextViews的xml部分

        <TextView
            android:id="@+id/frame1_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/colorchange"
            android:focusable="true"
            android:clickable="true"
            android:focusableInTouchMode="true"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/frame1_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/colorchange"
            android:focusable="true"
            android:clickable="true"
            android:focusableInTouchMode="true"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/white" />
        <TextView
            android:id="@+id/frame2_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/colorchange"
            android:focusableInTouchMode="true"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/frame2_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/colorchange"
            android:focusableInTouchMode="true"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="@color/white" />

这里是我的java代码:

private TextView currentTextView;

TextView Edit_frame1_1 = findViewById(R.id.frame1_1);
TextView Edit_frame1_2 = findViewById(R.id.frame1_2);
TextView Edit_frame2_1 = findViewById(R.id.frame2_1);
TextView Edit_frame2_2 = findViewById(R.id.frame2_2);

//noinspection AndroidLintClickableViewAccessibility
Edit_frame1_1.setOnTouchListener((view, event) -> {
    if(event.getAction() == MotionEvent.ACTION_UP){
        currentTextView = (TextView) view;
        return true;
    }
    return false;
    });

//noinspection AndroidLintClickableViewAccessibility
Edit_frame1_2.setOnTouchListener((view, event) -> {
    if(event.getAction() == MotionEvent.ACTION_UP){
        currentTextView = (TextView) view;
        return true;
    }
    return false;
});

Edit_frame2_1.setOnClickListener(view -> {
    currentTextView = (TextView) view;
});

Edit_frame2_2.setOnClickListener(view -> {
    currentTextView = (TextView) view;
});

现在我有前两个 TextViews 部分工作: 这些正在使用OnTouchListener,但不要切换回原来的背景(白色),它们只是保持灰色。

另外两个也在部分工作。它们将切换回白色背景,但我需要双击它们以获得通过按钮应用文本的焦点。

我不知道如何制作组合解决方案,使其按预期工作。

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    如果drawable按预期工作,那么唯一需要做的就是在关注另一个之前清除前一个TextView的焦点。

    TextView currentTextView = null;
    
    View.OnClickListener textViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(currentTextView == view)
                // It's the same TextView, so we don't need to do anything
                return;
            if(currentTextView != null) {
                // if currentTextView is not null, it contains the previously focused TextView
                // so clear it's focus here
            }
        
            // Here, request focus for the current TextView, using the `view` variable
    
            // and save it's reference to currentTextView variable
            currentTextView = (TextView) view;
        }
    });
    
    void yourMethod() {
        TextView textView1 = findViewById(R.id.textView1);
        textView1.setOnClickListener(textViewClickListener);
    
        // repeat for the rest of TextViews
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多