溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android組合式自定義控件如何實(shí)現(xiàn)購物車加減商品操作

發(fā)布時間:2021-08-04 14:38:15 來源:億速云 閱讀:94 作者:小新 欄目:移動開發(fā)

這篇文章主要為大家展示了“Android組合式自定義控件如何實(shí)現(xiàn)購物車加減商品操作”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android組合式自定義控件如何實(shí)現(xiàn)購物車加減商品操作”這篇文章吧。

具體內(nèi)容如下

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Addand mAddand;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mAddand= findViewById(R.id.add);
  
      mAddand.setOnNumberChangedListener(new Addand.OnNumberChangedListener() {
        @Override
        public void OnNumberChanged(int vs) {
          Toast.makeText(MainActivity.this, vs+"", Toast.LENGTH_SHORT).show();
        }
      });
    }
  }

activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"
  android:orientation="horizontal">

  <fanruiqi.www.com.jia.Addand
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

Addand.java

public class Addand extends FrameLayout implements View.OnClickListener{
  private ImageView mImage1;
  private ImageView mImage2;
  private TextView mText;
  int value;

  public Addand(@NonNull Context context) {
    this(context,null);
  }

  public Addand(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  public Addand(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    findView(context);
  }

  private void findView(Context context) {
    View view = View.inflate(context, R.layout.add, this);

     mImage1 =view.findViewById(R.id.image1);
     mImage2 = view.findViewById(R.id.image2);
     mText = view.findViewById(R.id.text);

     value=getValue();

     setValue(value);

     mImage1.setOnClickListener(this);
     mImage2.setOnClickListener(this);
  }
  private int vs=1;
  public int getValue() { //獲取值

    String trim = mText.getText().toString().trim();
    if (!TextUtils.isEmpty(trim)){
      Integer.valueOf(vs);
    }
    return vs;
  }

  public void setValue(int value) {
    mText.setText(value+"");
  }

  @Override
  public void onClick(View view) {

    switch (view.getId()){
      case R.id.image1:
        add();
        break;
      case R.id.image2:
        jian();
        break;
    }
  }

  private void jian() {
    if (vs>1){
      vs--;
      setValue(vs);
    }

    mOnNumberChangedListener.OnNumberChanged(vs);
  }

  private void add() {

    if (vs<6){
      vs++;
      setValue(vs);
    }

    mOnNumberChangedListener.OnNumberChanged(vs);
  }

  public interface OnNumberChangedListener{
    void OnNumberChanged(int vs);
  }

  private OnNumberChangedListener mOnNumberChangedListener;

  public void setOnNumberChangedListener(OnNumberChangedListener onNumberChangedListener){
      mOnNumberChangedListener=onNumberChangedListener;
  }

}

add.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">

  <ImageView
    android:id="@+id/image1"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:src="@drawable/ic_launcher_background"/>

  <TextView
    android:id="@+id/text"
    android:layout_width="50dp"
    android:layout_height="20dp"
    android:gravity="center"
    android:text="1"/>

  <ImageView
    android:id="@+id/image2"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:src="@drawable/ic_launcher_background"/>
</LinearLayout>

以上是“Android組合式自定義控件如何實(shí)現(xiàn)購物車加減商品操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI