布局做单选效果,选择金额充值(选择换背景,在选择其他的背景还原)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <LinearLayout
            android:weightSum="1"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TextView
                android:id="@+id/tv_30"
                android:text="30元"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:background="@drawable/recharge_money_box"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

        <TextView
                android:id="@+id/tv_50"
                android:text="50元"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/recharge_money_box"
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

        <TextView
                android:id="@+id/tv_100"
                android:text="100"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/recharge_money_box"
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

    </LinearLayout>

    <LinearLayout
            android:weightSum="1"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TextView
                android:id="@+id/tv_150"
                android:text="150"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/recharge_money_box"
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

        <TextView
                android:id="@+id/tv_300"
                android:text="300"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/recharge_money_box"
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

        <TextView
                android:id="@+id/other_price_tv"
                android:text="其他金额"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/recharge_money_box"
                android:gravity="center"
                android:layout_width="0dp"
                android:layout_weight="0.33"
                android:layout_height="45dp"/>

    </LinearLayout>

    <EditText
            android:id="@+id/other_price_et"
            android:hint="其他金额充值"
            android:layout_marginTop="10dp"
            android:visibility="gone"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>

    <Button
            android:id="@+id/pay_btn"
            android:text=" 充值"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>

</LinearLayout>


public class MyPrice {
	private String price;
	private int id;

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	
	
}


public class MyActivity extends Activity implements View.OnClickListener {
    private TextView mTv30;
    private TextView mTv50;
    private TextView mTv100;
    private TextView mTv150;
    private TextView mTv300;
    private TextView mOtherPriceTv;
    private EditText mOtherPriceEt;
    private Button mPayBtn;
    private List<TextView> mList;
	private Button pay_btn;
	private MyPrice myPrice;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }

    private void initView() {
        mTv30 = (TextView) findViewById(R.id.tv_30);
        mTv50 = (TextView) findViewById(R.id.tv_50);
        mTv100 = (TextView) findViewById(R.id.tv_100);
        mTv150 = (TextView) findViewById(R.id.tv_150);
        mTv300 = (TextView) findViewById(R.id.tv_300);
        pay_btn = (Button) findViewById(R.id.pay_btn);
        mOtherPriceTv = (TextView) findViewById(R.id.other_price_tv);
        mOtherPriceEt = (EditText) findViewById(R.id.other_price_et);
        mPayBtn = (Button) findViewById(R.id.pay_btn);
        setListener();
    }

    private void setListener() {
        mTv30.setOnClickListener(this);
        mTv50.setOnClickListener(this);
        mTv100.setOnClickListener(this);
        mTv150.setOnClickListener(this);
        mTv300.setOnClickListener(this);
        pay_btn.setOnClickListener(this);
        mOtherPriceTv.setOnClickListener(this);
        mPayBtn.setOnClickListener(this);
        addList();
    }

    private void addList() {
        mList = new ArrayList();
        mList.add(mTv30);
        mList.add(mTv50);
        mList.add(mTv100);
        mList.add(mTv150);
        mList.add(mTv300);
        mList.add(mOtherPriceTv);
    }

    private void setTextViewStyle(TextView v,Drawable d){
        for (int i = 0; i <mList.size() ; i++) {
            mList.get(i).setBackgroundResource(R.drawable.recharge_money_box);
        }
        v.setBackgroundDrawable(d);
        mOtherPriceEt.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_30:
                setTextViewStyle(mTv30,getResources().getDrawable(R.drawable.recharge_box_press));
                String m_Tv30 = mTv30.getText().toString();
                myPrice = new MyPrice();
                myPrice.setPrice(m_Tv30);
                myPrice.setId(R.id.tv_30);
                break;
            case R.id.tv_50:
                setTextViewStyle(mTv50,getResources().getDrawable(R.drawable.recharge_box_press));
                String m_Tv50 = mTv50.getText().toString();
                myPrice = new MyPrice();
                myPrice.setPrice(m_Tv50);
                myPrice.setId(R.id.tv_50);
                break;
            case R.id.tv_100:
                setTextViewStyle(mTv100,getResources().getDrawable(R.drawable.recharge_box_press));
                String m_Tv100 = mTv100.getText().toString();
                myPrice = new MyPrice();
                myPrice.setPrice(m_Tv100);
                myPrice.setId(R.id.tv_100);
                break;
            case R.id.tv_150:
                setTextViewStyle(mTv150,getResources().getDrawable(R.drawable.recharge_box_press));
                String m_Tv150 = mTv150.getText().toString();
                myPrice = new MyPrice();
                myPrice.setPrice(m_Tv150);
                myPrice.setId(R.id.tv_150);
                break;
            case R.id.tv_300:
                setTextViewStyle(mTv300,getResources().getDrawable(R.drawable.recharge_box_press));
                String m_Tv300 = mTv300.getText().toString();
                myPrice = new MyPrice();
                myPrice.setPrice(m_Tv300);
                myPrice.setId(R.id.tv_300);
                break;
            case R.id.other_price_tv:
                setTextViewStyle(mOtherPriceTv,getResources().getDrawable(R.drawable.recharge_box_press));
                mOtherPriceEt.setVisibility(View.VISIBLE);
                myPrice.setId(R.id.other_price_tv);
                break;
            case R.id.pay_btn:
                sumbit(v);
                break;
        }
    }

	private void sumbit(View v) {
		int id = myPrice.getId();
		if (id == R.id.other_price_tv) {
			String m_OtherPriceEt = mOtherPriceEt.getText().toString().trim();
			Toast.makeText(getBaseContext(), m_OtherPriceEt, 0).show();
			mOtherPriceEt.setText("");
		}else {
			String price = myPrice.getPrice();
			Toast.makeText(getBaseContext(), price, 0).show();
		}
		
	}
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值