如何在滚动视图中触摸按钮时更改按钮的颜色?

时间:2022-11-21 08:40:17

I am trying to make a scrollview which contains 3 buttons, and I want to allow these three buttons to change color onTouch;however, when I tried to set an onTouchListener, the button, when touched, changed color, but when I lifted my finger, it didn't change. Is there anyway I can do this?

我正在尝试制作一个包含3个按钮的滚动视图,我想让这三个按钮改变颜色onTouch;但是,当我尝试设置onTouchListener时,按钮在触摸时会改变颜色,但是当我抬起手指时,它没有改变。无论如何我能做到吗?

Here is an example of an onTouch and onClick Listener I have:

以下是我拥有的onTouch和onClick Listener的示例:

callButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            callButton.setBackgroundColor(Color.parseColor("#b20000"));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            callButton.setBackground(getDrawable(R.drawable.buttonshape1));
        }

        return false;
    }
});

callButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String phno = "314-605-4110";
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phno));
        startActivity(intent);
    }
});

And here is my XML for the scrollview:

这是我的用于scrollview的XML:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       tools:context="com.google.helpstl2.VariableLocation">
       <ScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           >

           <RelativeLayout
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" android:orientation="vertical">


               <Button
                   android:id="@+id/callButton"
                   android:textAllCaps="false"
                   android:text="Give us a call "
                   android:textColor="#4E4E4E"
                   android:textSize="15sp"
                   android:background="@drawable/buttonshape"
                   android:gravity="center_vertical|center_horizontal"
                   android:paddingLeft="130dp"
                   android:paddingRight="130dp"
                   android:drawablePadding="-8dp"
                   android:layout_width="match_parent"
                   android:layout_height="47dp"
                   android:layout_below="@+id/imageOverlay"

                   android:onClick="phoneClick"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_alignParentRight="true"
                   android:layout_alignParentEnd="true" />




               <LinearLayout
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@drawable/mapbackground"

                   android:id="@+id/mapLay"


                   android:layout_alignParentRight="true"
                   android:layout_alignParentEnd="true"
                   android:layout_below="@+id/callButton"
                   android:weightSum="1"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp"
                   android:layout_marginTop="9dp">


                   <fragment
                       android:layout_width="match_parent"
                       android:layout_height="220dp"
                       android:name="com.google.android.gms.maps.MapFragment"
                       android:id="@+id/placeMap"
                       android:background="@drawable/mapbackground"
                       android:layout_alignParentLeft="true"
                       android:layout_alignParentStart="true"
                       android:layout_gravity="center_vertical"


                       android:duplicateParentState="true"
                       android:layout_margin="2dp"
                       android:layout_marginBottom="50dp" />
               </LinearLayout>

               <Button
                   android:id="@+id/directionsButton"
                   android:textAllCaps="false"
                   android:text="Come see us"
                   android:textColor="#000000"
                   android:textSize="14sp"
                   android:paddingLeft="10dp"
                   android:gravity="center_vertical"
                   android:drawablePadding="10dp"

                   android:layout_width="match_parent"
                   android:layout_height="44dp"
                   android:background="@drawable/buttonshape1"

                   android:onClick="directionsClick"
                   android:singleLine="false"
                   android:layout_marginTop="9dp"
                   android:layout_below="@+id/mapLay"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp" />

               <Button
                   android:id="@+id/moreInfoButton"
                   android:textAllCaps="false"
                   android:text="More Info"
                   android:textColor="#000000"
                   android:textSize="14sp"
                   android:gravity="center_vertical"
                   android:paddingLeft="10dp"
                   android:drawablePadding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="44dp"
                   android:background="@drawable/buttonshape2"
                   android:onClick="directionsClick"
                   android:singleLine="false"
                   android:layout_below="@+id/directionsButton"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp" />




           </RelativeLayout>
       </ScrollView>


   </RelativeLayout>

2 个解决方案

#1


If you want to change the color permanently then you should just remove View.TouchListener implementation and do it in button click like

如果你想永久地改变颜色,那么你应该只删除View.TouchListener实现并按下按钮点击它

callButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        callButton.setBackgroundColor(Color.parseColor("#b20000"));
    }
});

If you want to change the color only when the button is pressed, you should do it in the drawable (@drawable/buttonshape). For example this element changes the color to blue when pressed.

如果只想在按下按钮时更改颜色,则应在drawable(@ drawable / buttonshape)中进行更改。例如,此元素在按下时将颜色更改为蓝色。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape><solid android:color="@color/blue" /></shape>
    </item>
    <item>
        <shape><solid android:color="@color/white"/></shape>
    </item>
</selector>

You shouldn't implement onTouchListener for this.

您不应该为此实现onTouchListener。

#2


Create this selector_button.xml in your drawable:

在drawable中创建这个selector_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--button color pressed -->
<item android:drawable="@android:color/black" android:state_pressed="true"></item>
<!--button color normal -->
<item android:drawable="@android:color/transparent"></item></selector>

And your button layout :

你的按钮布局:

 <Button
    android:id="@+id/bt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_button"
    android:text="abc"
/>

#1


If you want to change the color permanently then you should just remove View.TouchListener implementation and do it in button click like

如果你想永久地改变颜色,那么你应该只删除View.TouchListener实现并按下按钮点击它

callButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        callButton.setBackgroundColor(Color.parseColor("#b20000"));
    }
});

If you want to change the color only when the button is pressed, you should do it in the drawable (@drawable/buttonshape). For example this element changes the color to blue when pressed.

如果只想在按下按钮时更改颜色,则应在drawable(@ drawable / buttonshape)中进行更改。例如,此元素在按下时将颜色更改为蓝色。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape><solid android:color="@color/blue" /></shape>
    </item>
    <item>
        <shape><solid android:color="@color/white"/></shape>
    </item>
</selector>

You shouldn't implement onTouchListener for this.

您不应该为此实现onTouchListener。

#2


Create this selector_button.xml in your drawable:

在drawable中创建这个selector_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--button color pressed -->
<item android:drawable="@android:color/black" android:state_pressed="true"></item>
<!--button color normal -->
<item android:drawable="@android:color/transparent"></item></selector>

And your button layout :

你的按钮布局:

 <Button
    android:id="@+id/bt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_button"
    android:text="abc"
/>