自定義LinearLayout實現加減以及長按持續增加和減少
阿新 • • 發佈:2018-12-22
public class UpDownChooseView extends LinearLayout implements View.OnClickListener{ private Context context; private FrameLayout zhi_yin_decrease,zhi_yin_add; private EditText txt_show; private int defaultValue=1; private int defaultMaxValue=100; private int defaultMinValue=0; private int defaultStep=1; private OnUpDownSelectListener onUpDownSelectListener; public static final int TYPE_UP = 1; public static final int TYPE_DOWN = 2; public static final int TYPE_TEXT_CHANGE = 3; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what==TYPE_UP){ if (defaultValue+defaultStep<=defaultMaxValue){ defaultValue=defaultValue+defaultStep; txt_show.setText(defaultValue+(isPercent?"%":"")); if (onUpDownSelectListener!=null){ onUpDownSelectListener.onUpDown(defaultValue); } handler.sendEmptyMessageDelayed(TYPE_UP,80); } }else if (msg.what==TYPE_DOWN){ if (defaultValue-defaultStep>=defaultMinValue){ defaultValue=defaultValue-defaultStep; if (onUpDownSelectListener!=null){ onUpDownSelectListener.onUpDown(defaultValue); } if (defaultValue>0){ txt_show.setText(defaultValue+(isPercent?"%":"")); }else { txt_show.setText("不設定"); } handler.sendEmptyMessageDelayed(TYPE_DOWN,80); } } } }; public void setOnUpDownSelectListener(OnUpDownSelectListener onUpDownSelectListener) { this.onUpDownSelectListener = onUpDownSelectListener; } private boolean isPercent=false,textEnable = false; public UpDownChooseView(Context context) { super(context); this.context=context; initView(); } public UpDownChooseView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context=context; initView(); } private void initView() { View contentView= LayoutInflater.from(context).inflate(R.layout.item_updown_choose,null,false); zhi_yin_decrease=contentView.findViewById(R.id.zhi_yin_decrease); zhi_yin_add=contentView.findViewById(R.id.zhi_yin_add); txt_show=contentView.findViewById(R.id.txt_show); zhi_yin_decrease.setOnClickListener(this); zhi_yin_add.setOnClickListener(this); zhi_yin_decrease.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { handler.sendEmptyMessage(TYPE_DOWN); return true; } }); zhi_yin_decrease.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP){ handler.removeMessages(TYPE_DOWN); } return false; } }); zhi_yin_add.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { handler.sendEmptyMessage(TYPE_UP); return true; } }); zhi_yin_add.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP){ handler.removeMessages(TYPE_UP); } return false; } }); addView(contentView); } @Override public void onClick(View v) { if (v==zhi_yin_decrease){ if (defaultValue-defaultStep>=defaultMinValue){ setDefaultText(defaultValue-defaultStep,isPercent); } }else if (v==zhi_yin_add){ if (defaultValue+defaultStep<=defaultMaxValue){ setDefaultText(defaultValue+defaultStep,isPercent); } } } public void setDefaultText(int value,boolean isPercent){ if (value>0){ defaultValue=value; this.isPercent=isPercent; txt_show.setText(defaultValue+(isPercent?"%":"")); if (onUpDownSelectListener!=null){ onUpDownSelectListener.onUpDown(defaultValue); } }else if (value==0){ defaultValue=value; this.isPercent=isPercent; txt_show.setText("不設定"); if (onUpDownSelectListener!=null){ onUpDownSelectListener.onUpDown(defaultValue); } } } public int getDefaultValue() { return defaultValue; } public void setDefaultValue(int defaultValue) { this.defaultValue = defaultValue; } public int getDefaultMaxValue() { return defaultMaxValue; } public void setDefaultMaxValue(int defaultMaxValue) { this.defaultMaxValue = defaultMaxValue; } public int getDefaultMinValue() { return defaultMinValue; } public void setDefaultMinValue(int defaultMinValue) { this.defaultMinValue = defaultMinValue; } public int getDefaultStep() { return defaultStep; } public void setDefaultStep(int defaultStep) { this.defaultStep = defaultStep; } //這個介面用於獲取資料的 public interface OnUpDownSelectListener { public void onUpDown(int value); } }
其中裡面涉及的一個佈局layout.item_updown_choose如下
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_updown_round"> <FrameLayout android:id="@+id/zhi_yin_decrease" android:layout_width="35dip" android:layout_height="35dip" android:background="@drawable/decrease_shap" > <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#FFFD7C7A"/> </FrameLayout> <EditText android:id="@+id/txt_show" android:layout_width="90dip" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="10" android:inputType="number" android:enabled="false" android:textColor="#323232" android:imeOptions="actionDone" android:background="@null" android:cursorVisible="false" android:textSize="13dip" /> <FrameLayout android:id="@+id/zhi_yin_add" android:layout_width="35dip" android:layout_height="35dip" android:layout_alignParentRight="true" android:background="@drawable/add_shap" > <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#FFFD7C7A"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:background="#FFFD7C7A"/> </FrameLayout> </RelativeLayout>
其中的一些shape檔案就不一一貼出來了,大家根據自己的需求可以自己寫的
上面是工具,下面是如何使用
public class MainActivity extends AppCompatActivity implements UpDownChooseView.OnUpDownSelectListener{ UpDownChooseView up_down; TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); up_down=findViewById(R.id.up_down); up_down.setOnUpDownSelectListener(this); txt=findViewById(R.id.txt); up_down.setDefaultText(10,true); } @Override public void onUpDown(int value) { txt.setText(value+""); } }
效果圖
點選加減,以及長按都可實習資料的變化。