【发布时间】: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,但不要切换回原来的背景(白色),它们只是保持灰色。
另外两个也在部分工作。它们将切换回白色背景,但我需要双击它们以获得通过按钮应用文本的焦点。
我不知道如何制作组合解决方案,使其按预期工作。
【问题讨论】: