获取控件enable状态_Android自定义组合控件数字加减(适用于购物车)
生活随笔
收集整理的這篇文章主要介紹了
获取控件enable状态_Android自定义组合控件数字加减(适用于购物车)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家好,我是小黑,一個還沒禿頭的程序員~~~
獨學而無友,則孤陋而寡聞--《禮記·學記》
今天的內容是自定義一個數組加減的控件,可以應用于購物車的數量選擇,效果如下:
自定義實現了控件的默認值、最大值、最小值、步長的值的設置
(一)設置控件布局view_number.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/icon_add_enable" /> <EditText android:id="@+id/edit_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/frame_with_gray_edge_white" android:paddingLeft="15dp" android:paddingTop="10dp" android:paddingRight="15dp" android:paddingBottom="10dp" android:text="1" android:textColor="#000" android:textSize="14dp" /> <ImageView android:id="@+id/iv_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/select_minus_switch" />LinearLayout>按鈕樣式只實現了可不可點擊狀態的切換,大家也可自己實現單擊狀態的效果select_minus_switch.xml代碼如下:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/icon_minus_disable" android:state_enabled="false" /> <item android:drawable="@mipmap/icon_minus_enable" android:state_enabled="true" />selector>frame_with_gray_edge_white.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="5dp" /> <solid android:color="#fff" /> <stroke android:width="0.5dp" android:color="#c0c0c0" />shape>(二)創建類并繼承基本布局,加載視圖,自定義屬性設計獲取以及使用自定義屬性文件attrs_number_view.xml代碼如下:<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="NumberView"> <attr name="min" format="integer" /> <attr name="max" format="integer" /> <attr name="step" format="integer" /> <attr name="defaultValue" format="integer" /> declare-styleable>resources>NumberView.java代碼如下:
public class NumberView extends LinearLayout { private ImageView mIvAdd; private ImageView mIvMinus; private EditText mEditValue; private int mCurrentValue;//當前的數值????private?OnValueChangeListener?mOnValueChangeListener;//值發生變化時的回調????private?int?mMax;//最大值????private?int?mMin;//最小值????private?int?mStep;//步長????private?int?mDefaultValue;//默認值???? public int getMax() { return mMax; } public void setMax(int max) { mMax = max; } public int getMin() { return mMin; } public void setMin(int min) { mMin = min; } public int getStep() { return mStep; } public void setStep(int step) { mStep = step; } public int getDefaultValue() { return mDefaultValue; } public void setDefaultValue(int defaultValue) { mCurrentValue = mDefaultValue = defaultValue; updateText(); } public int getCurrentValue() { return mCurrentValue; }????//手動設置值需要更新UI public void setCurrentValue(int currentValue) { mCurrentValue = currentValue; updateText(); } public NumberView(Context context) { this(context, null, 0); } public NumberView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public NumberView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); getAttrs(context, attrs); }????//獲取自定義屬性 private void getAttrs(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberView); mMax = typedArray.getInt(R.styleable.NumberView_max, 999999); mMin = typedArray.getInt(R.styleable.NumberView_min, 0); mStep = typedArray.getInt(R.styleable.NumberView_step, 1); mDefaultValue = typedArray.getInt(R.styleable.NumberView_defaultValue, 1);????????mCurrentValue?=?mDefaultValue;//當前值等于默認值????????if?(mCurrentValue?==?mMin)?{//當前值為最小值時減號不能點擊 mIvMinus.setEnabled(false); } else { mIvMinus.setEnabled(true); } }????//加載布局,定義控件以及設置監聽 private void initView(Context context) { View inflate = LayoutInflater.from(context).inflate(R.layout.view_number, this, false); this.addView(inflate); mIvAdd = inflate.findViewById(R.id.iv_add); mIvMinus = inflate.findViewById(R.id.iv_minus); mEditValue = inflate.findViewById(R.id.edit_value); mIvAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {???????????????//先加完再比較,只要一點加號,減號就可以點擊了 mCurrentValue += mStep; mIvMinus.setEnabled(true);????????????????//為了防止超過最大值,最后一步將最大值設置成當前值 if (mCurrentValue >= mMax) { mCurrentValue = mMax; mIvAdd.setEnabled(false); }????????????????//更新UI updateText(); //回調當前值 if (mOnValueChangeListener != null) { mOnValueChangeListener.onValueChange(mCurrentValue); } } }); mIvMinus.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //減號與加號同理 mCurrentValue -= mStep; mIvAdd.setEnabled(true); if (mCurrentValue <= mMin) { mCurrentValue = mMin; mIvMinus.setEnabled(false); } updateText(); if (mOnValueChangeListener != null) { mOnValueChangeListener.onValueChange(mCurrentValue); } } }); } private void updateText() { mEditValue.setText(mCurrentValue + ""); } public interface OnValueChangeListener { void onValueChange(int value); } public void setOnValueChangeListener(OnValueChangeListener onValueChangeListener) { mOnValueChangeListener = onValueChangeListener; }在Activity中的控件使用:
NumberView numberView = findViewById(R.id.view_number); numberView.setOnValueChangeListener(new NumberView.OnValueChangeListener() { @Override public void onValueChange(int value) { Toast.makeText(NumberViewActivity.this, "The current value is :"+value, Toast.LENGTH_SHORT).show();????????????}????????});到此為止,一個關于數字加減選擇的自定義控件就已經完成了,是不是很簡單,注釋也寫得很清楚,大家也可以自己添加各種自定義屬性,如數值的精度以及按鈕的樣式等等,最后,祝大家身體健康,萬事如意,感謝大家的支持與閱讀!
總結
以上是生活随笔為你收集整理的获取控件enable状态_Android自定义组合控件数字加减(适用于购物车)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编译期java_java编译期和运行期
- 下一篇: java 如何循环执行一个对象_Java