[英]View.setBackgroundResource() is not setting the correct background resource
I am coding a "set pin" fragment for the second time and it's not working.我正在第二次编写“设置引脚”片段,但它不起作用。 Everything works great until I use
switch
blocks and View.OnTouchListener
.在我使用
switch
块和View.OnTouchListener
之前,一切都很好。 I am trying to change the buttons background resource to a different drawable when pressed down and then back when finger is lifted.我试图在按下时将按钮背景资源更改为不同的可绘制对象,然后在抬起手指时将其返回。
My problem is that the background resource is not changing at all.我的问题是后台资源根本没有改变。
XML for layout: XML 用于布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/tertiary_dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Design.NoActionBar"
>
<View
android:id="@+id/divider3"
android:layout_width="364.059dp"
android:layout_height="2dp"
android:background="@color/secondary_accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tableLayout" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/pin7"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:background="@drawable/button_1_background_idle"
android:text="7"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin8"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="8"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin9"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="9"
android:textColor="@color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/pin4"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="4"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin5"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="5"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin6"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="6"
android:textColor="@color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/pin1"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="1"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin2"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="2"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin3"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="3"
android:textColor="@color/primary_light"
android:textSize="22sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/pina"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="A"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pin0"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="0"
android:textColor="@color/primary_light"
android:textSize="22sp" />
<Button
android:id="@+id/pinb"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="B"
android:textColor="@color/primary_light"
android:textSize="22sp" />
</TableRow>
</TableLayout>
<View
android:id="@+id/view"
android:layout_width="364.059dp"
android:layout_height="75dp"
android:layout_marginTop="104dp"
android:background="@color/primary_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.489"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/slatebook"
android:text="1234567890ab"
android:textColor="@color/primary_light"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="@+id/view"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintStart_toStartOf="@+id/view"
app:layout_constraintTop_toTopOf="@+id/view" />
<View
android:id="@+id/divider"
android:layout_width="364.059dp"
android:layout_height="2dp"
android:background="@color/secondary_accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code for java class: java class 的代码:
package com.example.portal;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.system.StructUtsname;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import java.util.ArrayList;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.login_layout, container, false);
}
@SuppressLint("ClickableViewAccessibility")
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
load();
}
@SuppressLint("ClickableViewAccessibility")
private void load() {
ArrayList<Button> pinbtns = new ArrayList<>();
ArrayList<Character> vals = new ArrayList<>();
pinbtns.add((Button) getActivity().findViewById(R.id.pin0));
pinbtns.add((Button) getActivity().findViewById(R.id.pin1));
pinbtns.add((Button) getActivity().findViewById(R.id.pin2));
pinbtns.add((Button) getActivity().findViewById(R.id.pin3));
pinbtns.add((Button) getActivity().findViewById(R.id.pin4));
pinbtns.add((Button) getActivity().findViewById(R.id.pin5));
pinbtns.add((Button) getActivity().findViewById(R.id.pin6));
pinbtns.add((Button) getActivity().findViewById(R.id.pin7));
pinbtns.add((Button) getActivity().findViewById(R.id.pin8));
pinbtns.add((Button) getActivity().findViewById(R.id.pin9));
pinbtns.add((Button) getActivity().findViewById(R.id.pina));
pinbtns.add((Button) getActivity().findViewById(R.id.pinb));
vals.add('0');
vals.add('1');
vals.add('2');
vals.add('3');
vals.add('4');
vals.add('5');
vals.add('6');
vals.add('7');
vals.add('8');
vals.add('9');
vals.add('a');
vals.add('b');
for (int r = 0; r < pinbtns.size(); r++) {
Button btn = pinbtns.get(r);
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
btn.setBackgroundResource(R.drawable.button_1_background_pressed);
System.out.println("down");
return true;
case MotionEvent.ACTION_UP:
Thread t = new Thread(() -> {
try {
Thread.sleep(50);
} catch (Exception ig) {
ig.printStackTrace();
}
getActivity().runOnUiThread(() -> {
btn.setBackgroundResource(R.drawable.button_1_background_idle);
});
});
t.start();
System.out.println("up");
return true;
}
return false;
}
});
}
}
}
Code for drawables:可绘制代码:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/primary_dark"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/primary_accent"/>
</shape>
And the color resourcefile for good measure.和颜色资源文件可以很好地衡量。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary_light">#dcdcdc </color>
<color name="primary_dark">#1c1d1f</color>
<color name="secondary_dark">#151515</color>
<color name="tertiary_dark">#323335</color>
<color name="secondary_light">#9ea09f</color>
<color name="primary_accent">#c41442</color>
<color name="secondary_accent">#711930</color>
</resources>
Just to clarify one more time.只是为了再澄清一次。 When I press a button (for example pin7) the system prints "down" to the console but the background does not change one bit.
当我按下一个按钮(例如 pin7)时,系统将“向下”打印到控制台,但背景不会改变一点。 When I release I get "up" in the console.
当我释放时,我在控制台中“起来”。 So it works as intended except for the background.
所以它按预期工作,除了背景。
I have tried changing the setBacgroundResource
to setBackroundColor
and same outcome.我尝试将
setBacgroundResource
更改为setBackroundColor
和相同的结果。
I have spent 2 hours and am trying to find out why the setBackgroundResource()
is not working.我花了 2 个小时试图找出
setBackgroundResource()
不起作用的原因。 I added some modification on the shape and I noticed that the change only made on the dimensions except the Color.我对形状添加了一些修改,我注意到除了颜色之外只对尺寸进行了更改。 So I created a style to eliminate the Tinte I put the
backgroundTint
on @null
and everything works correctly.因此,我创建了一种样式来消除 Tinte,我将
backgroundTint
放在@null
上,一切正常。
here is the style I added这是我添加的样式
<style name="NotTintedButton" parent="Widget.AppCompat.Button.Colored">
<item name="backgroundTint">@null</item>
</style>
And all you have to do is apply this style on each buttons.您所要做的就是在每个按钮上应用这种样式。 for example:
例如:
<Button
style="@style/NotTintedButton"
android:id="@+id/pin8"
android:layout_width="@dimen/button_1_width"
android:layout_height="@dimen/button_1_height"
android:layout_margin="@dimen/button_1_margin"
android:background="@color/primary_dark"
android:fontFamily="@font/slatebook"
android:padding="0dp"
android:text="8"
android:textColor="@color/primary_light"
android:textSize="22sp" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.