Android組合式自定義控件實(shí)現(xiàn)購(gòu)物車加減商品操作
本文實(shí)例為大家分享了Android實(shí)現(xiàn)購(gòu)物車加減商品操作的具體代碼,供大家參考,具體內(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android通過(guò)自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
通過(guò)自定義ImageView控件,在xml布局里面調(diào)用自定的組件實(shí)現(xiàn)圖片的縮放。下面給大家分享實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2016-10-10
Android之淘寶商品列表長(zhǎng)按遮罩效果的實(shí)現(xiàn)
這篇文章主要介紹了Android之淘寶商品列表長(zhǎng)按遮罩效果的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android開(kāi)發(fā)系列二之窗口Activity的生命周期
這篇文章主要介紹了Android學(xué)習(xí)系列二之窗口Activity的生命周期的相關(guān)資料,需要的朋友可以參考下2016-05-05
Android實(shí)現(xiàn)帶磁性的懸浮窗體效果
這篇文章主要介紹了Android實(shí)現(xiàn)帶磁性的懸浮窗體效果,涉及Android針對(duì)窗體的動(dòng)態(tài)操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Flutter仿微信通訊錄實(shí)現(xiàn)自定義導(dǎo)航條的示例代碼
某些頁(yè)面比如我們?cè)谶x擇聯(lián)系人或者某個(gè)城市的時(shí)候需要快速定位到我們需要的選項(xiàng),一般都會(huì)需要像微信通訊錄右邊有一個(gè)導(dǎo)航條一樣的功能,本文將利用Flutter實(shí)現(xiàn)這一效果,需要的可以參考一下2022-04-04
Android PC端用ADB抓取指定應(yīng)用日志實(shí)現(xiàn)步驟
這篇文章主要介紹了Android PC端用ADB抓取指定應(yīng)用日志實(shí)現(xiàn)步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Android studio實(shí)現(xiàn)加法軟件
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)加法軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

