android 商城常用控件,android 自定义商城app价格正序倒序控件

本文档展示了如何创建一个名为PriceUpDownView的自定义Android视图,该视图显示价格并允许点击切换上下箭头。通过设置回调接口,可以获取点击状态。在使用示例中,PriceUpDownView被集成到布局中,并设置了点击监听事件,当用户点击时,会弹出提示显示价格是上涨还是下降。
摘要由CSDN通过智能技术生成

1.效果图如下:

2b16d9dcdefa4480746ab020a5460057.gif

2.自定义视图布局文件 price_up_down.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="价格"

android:textColor="#a4a4a4"

android:id="@+id/price_id"/>

3.自定义视图代码 PriceUpDownView.java

package net.sytm.priceupdowndemo;

import android.content.Context;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.TextView;

/**

* Created by aoc on 2017/4/11.

*/

public class PriceUpDownView extends LinearLayout implements View.OnClickListener {

private TextView priceView;

private boolean isUp;

private Callback callback;

public PriceUpDownView(Context context) {

this(context, null);

}

public PriceUpDownView(Context context, @Nullable AttributeSet attrs) {

this(context, attrs, 0);

}

public PriceUpDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initView(context);

}

private void initView(Context context) {

LayoutInflater.from(context).inflate(R.layout.price_up_down, this);

priceView = (TextView) findViewById(R.id.price_id);

priceView.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.price_id:

setDrawable();

callback.getStatus(isUp);

break;

}

}

private void setDrawable() {

Drawable drawable = null;

if (isUp) {

isUp = false;

drawable = getResources().getDrawable(R.drawable.xia);

}

else {

isUp = true;

drawable = getResources().getDrawable(R.drawable.shang);

}

drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

priceView.setCompoundDrawables(null, null, drawable, null);

priceView.setCompoundDrawablePadding(8);

priceView.setTextColor(Color.parseColor("#e02b4d"));

}

public interface Callback {

void getStatus(boolean isUp);

}

public void setCallback(Callback callback) {

this.callback = callback;

}

}

4.使用示例

a.视图布局引用

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="net.sytm.priceupdowndemo.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/price_up_down_id">

b.代码

package net.sytm.priceupdowndemo;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements PriceUpDownView.Callback {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

PriceUpDownView upDownView = (PriceUpDownView) findViewById(R.id.price_up_down_id);

upDownView.setCallback(this);

}

@Override

public void getStatus(boolean isUp) {

ToastUtil.showShort(this, isUp ? "上" : "下");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值