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

Java 在滚动视图中,触摸按钮时,如何更改按钮的颜色?,java,android,xml,scrollview,Java,Android,Xml,Scrollview,我正在尝试制作一个包含3个按钮的scrollview,我想允许这三个按钮在触摸屏上改变颜色;然而,当我试图设置一个onTouchListener时,按钮在被触摸时改变了颜色,但当我抬起手指时,它没有改变。还有什么我可以做的吗 下面是我拥有的onTouch和onClick侦听器示例: callButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v,

我正在尝试制作一个包含3个按钮的scrollview,我想允许这三个按钮在触摸屏上改变颜色;然而,当我试图设置一个onTouchListener时,按钮在被触摸时改变了颜色,但当我抬起手指时,它没有改变。还有什么我可以做的吗

下面是我拥有的onTouch和onClick侦听器示例:

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);
    }
});
下面是我在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>

如果要永久更改颜色,只需删除View.TouchListener实现,然后在按钮中单击“喜欢”即可

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

如果只想在按下按钮时更改颜色,则应在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>


您不应该为此实现onTouchListener

在绘图文件中创建此选择器\u 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>

以及您的按钮布局:

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


输出是什么?是否仅在等待时更改?@Nabin当我触摸按钮并松开手指时,按钮不会恢复到原来的颜色。感谢您的回答,但当我尝试您的方法时,没有onTouch侦听器,按钮在加载活动时已更改颜色。您好,感谢您的回答,但当我尝试您的第二种方法时,如果没有onTouch侦听器,则在加载活动时按钮已经改变了颜色。对于每个按钮,您已经有了名为buttonshape的背景绘图。你是怎么编码的?州的颜色已经在里面编码了吗?谢谢,我实际上只需要复制你的代码并替换我现有的代码。