如何突出一个按钮时,pressed? [英] How to highlight a button when is pressed?

查看:158
本文介绍了如何突出一个按钮时,pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Andorid的测验,我想强调的一个按钮时,它的点击,但是当用户让去,它变成了它本来的颜色的按钮。你看,我已经设置按钮的背景,使该按钮可以是圆形的。我给自己定,在绘制。

 <按钮
    机器人:ID =@ + ID / BTN1
    机器人:背景=@可绘制/ roundedbutton
    机器人:文字颜色=#FFFFFF
    机器人:TEXTSTYLE =斜体
    机器人:layout_width =225sp
    机器人:layout_marginTop =23sp
    机器人:layout_height =38sp
    机器人:layout_alignLeft =@ + ID / BTN2
    机器人:layout_below =@ + ID / textView1
    机器人:文本=按钮/>
 

roundedbutton.xml

 < XML版本=1.0编码=UTF-8&GT?;
 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:形状=矩形机器人:填充=10dp>
[固体机器人:颜色=#848482/> <! - 这一个是圆形按钮的部份效果颜色 - >

<角落
机器人:bottomRightRadius =6.5dp
机器人:bottomLeftRadius =6.5dp
机器人:topLeftRadius =6.5dp
机器人:topRightRadius =6.5dp/>
< /形状>
 

解决方案

您可以使用 OnTouchListener ,也可以使用一个选择。

  button.setOnTouchListener(新OnTouchListener(){

@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
    如果(event.getAction()== MotionEvent.ACTION_DOWN){
            //改变颜色
    }
    否则如果(event.getAction()== MotionEvent.ACTION_UP){
            //设置为正常颜色
    }

    返回true;
}
});
 

您可以使用一个选择器也。边框和圆角矩形。自定义相同。

bkg.xml在绘制文件夹

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:STATE_ pressed =真
        机器人:可绘制=@可绘制/ pressed/>
    <项目安卓state_focused =假
        机器人:可绘制=@可绘制/正常/>
< /选择器>
 

normal.xml在绘制文件夹

 < XML版本=1.0编码=UTF-8&GT?;
  <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>
  [固体机器人:颜色=#0AECBF/>
  <行程机器人:宽=3DP
        机器人:颜色=#0FECFF/>
  <填充的android:左=5DP
         机器人:顶部=5DP
         机器人:右=5DP
         机器人:底部=5DP/>
  <边角安卓bottomRightRadius =7DP
         机器人:bottomLeftRadius =7DP
         机器人:topLeftRadius =7DP
         机器人:topRightRadius =7DP/>
  < /形状>
 

pressed.xml在绘制文件夹

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:形状=矩形>
   [固体机器人:颜色=#ff33ffff/>
 <填充的android:左=5DP
             机器人:顶部=5DP
             机器人:右=5DP
             机器人:底部=5DP/>
    <边角安卓bottomRightRadius =7DP
             机器人:bottomLeftRadius =7DP
             机器人:topLeftRadius =7DP
             机器人:topRightRadius =7DP/>
< /形状>
 

现在设置背景来回的按钮,在XML

 安卓背景=@可绘制/ BKG
 

I'm making an Andorid quiz and I want to highlight a button when it's clicked but when the user lets go of the button that it turns in it original colour. You see I've set the background of the button so the buttons can be rounded. I've set that in drawable.

<Button
    android:id="@+id/btn1"
    android:background="@drawable/roundedbutton"
    android:textColor="#ffffff"
    android:textStyle="italic"
    android:layout_width="225sp"
    android:layout_marginTop="23sp"
    android:layout_height="38sp"
    android:layout_alignLeft="@+id/btn2"
    android:layout_below="@+id/textView1"
    android:text="Button" />

roundedbutton.xml

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" android:padding="10dp">   
<solid android:color="#848482"/> <!-- this one is ths color of the Rounded Button -->

<corners
android:bottomRightRadius="6.5dp"
android:bottomLeftRadius="6.5dp"
android:topLeftRadius="6.5dp"
android:topRightRadius="6.5dp"/>
</shape>

解决方案

You can use OnTouchListener or you can use a selector.

button.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // change color
    }
    else if (event.getAction() == MotionEvent.ACTION_UP) {
            // set to normal color
    }

    return true;
}
});

You can use a selector also. Borders and rounded rectangle. Customize the same.

bkg.xml in drawable folder

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

normal.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#0AECBF"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>  

pressed.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff33ffff" />
 <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
</shape>

Now set the background fro your button in xml

     android:background="@drawable/bkg"

这篇关于如何突出一个按钮时,pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆