Android – Button, 设置字体大小写,自定义点击效果, 双击监听
-
取消字母全部大写:
android:textAllCaps="false"
-
自定义点击效果(点击时颜色变化):
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape android:shape="rectangle"> <stroke android:color="@color/colorPrimary" android:width="1dp"/> <solid android:color="@color/colorPrimary"/> <corners android:radius="15dp"/> </shape> </item> <item android:state_pressed="true"> <shape android:shape="rectangle"> <stroke android:color="@color/colorTY_Half" android:width="1dp"/> <solid android:color="@color/colorTY_Half"/> <corners android:radius="15dp"/> </shape> </item> </selector>
-
双击监听
-
自定义接口:
public class OnDoubleClickListener implements View.OnTouchListener{ private int count = 0;//点击次数 private long firstClick = 0;//第一次点击时间 private long secondClick = 0;//第二次点击时间 private final int duration = 500;//预设两次点击最多间隔时间 private Callback callback; public interface Callback{ void onDoubleClick(); } public OnDoubleClickListener(Callback callback) { this.callback = callback; } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN){ count ++; if (count == 1){ firstClick = System.currentTimeMillis(); }else if (count == 2){ secondClick = System.currentTimeMillis(); if (secondClick - firstClick<=duration){ if (callback!=null){ callback.onDoubleClick();//调用重写方法 } count = 0; firstClick = 0; return true; }else { //间隔时间超过设定值,重新计算 firstClick = secondClick; count = 1; } secondClick = 0; } } //最后要返回false 否则单击事件会被屏蔽掉 return false; } }
-
调用:
view.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.Callback() { @Override public void onDoubleClick() { //do something } }));
-