Android小demo,两个button,控制多个输入框实现值自增自减。

如图,使用两个按钮控制九个输入框实现自增自减。从0开始



简单的线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.tdgeos.demo.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right" >
        <Button 
            android:id="@+id/btn_increment"
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="自增++"/>
        <Button 
            android:id="@+id/btn_reduce"
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="自减--"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText 
            android:id="@+id/et_input1"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input2"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input3"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText 
            android:id="@+id/et_input4"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input5"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input6"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText 
            android:id="@+id/et_input7"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input8"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
        <EditText 
            android:id="@+id/et_input9"
            android:layout_width="0dp"
            android:layout_weight="1"
        	android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

代码思路:给每个输入框加选中监听,将自身控件id传给两个button,点击按钮时,调用自增自减方法。

package com.tdgeos.demo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

	private Button btnIncrement = null;
	private Button btnReduce = null;
	private EditText etInput1;
	private EditText etInput2;
	private EditText etInput3;
	private EditText etInput4;
	private EditText etInput5;
	private EditText etInput6;
	private EditText etInput7;
	private EditText etInput8;
	private EditText etInput9;
	private List<EditText> list = new ArrayList<EditText>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initWidget();
	}


	private int editId; // 记录选中的输入框的id
	// 初始化控件
	private void initWidget() {
		btnIncrement = (Button) findViewById(R.id.btn_increment);
		btnIncrement.setOnClickListener(this);
		btnReduce = (Button) findViewById(R.id.btn_reduce);
		btnReduce.setOnClickListener(this);
		etInput1 = (EditText) findViewById(R.id.et_input1);
		etInput2 = (EditText) findViewById(R.id.et_input2);
		etInput3 = (EditText) findViewById(R.id.et_input3);
		etInput4 = (EditText) findViewById(R.id.et_input4);
		etInput5 = (EditText) findViewById(R.id.et_input5);
		etInput6 = (EditText) findViewById(R.id.et_input6);
		etInput7 = (EditText) findViewById(R.id.et_input7);
		etInput8 = (EditText) findViewById(R.id.et_input8);
		etInput9 = (EditText) findViewById(R.id.et_input9);
		list.add(etInput1);
		list.add(etInput2);
		list.add(etInput3);
		list.add(etInput4);
		list.add(etInput5);
		list.add(etInput6);
		list.add(etInput7);
		list.add(etInput8);
		list.add(etInput9);
		// 遍历输入框,给每个EditText加选中监听。
		for (EditText input : list) {
			input.setInputType(InputType.TYPE_NULL);
			input.setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						// 被选中时,得到该控件的id。
						editId = v.getId();
					}
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_increment:
			addSelf(editId);
			break;
		case R.id.btn_reduce:
			reduceSelf(editId);
			break;
		default:
			break;
		}
	}

	// 自减运算,传入控件id
	public void reduceSelf(int id) {
		EditText etjj = (EditText) findViewById(id);
		String etjjStr = etjj.getText().toString().trim();
		int zhi = 0;
		if (!TextUtils.isEmpty(etjjStr)) {
			zhi = Integer.parseInt(etjjStr);
		} else {
			zhi = 0;
		}
		zhi = zhi - 1;
		if (zhi < 0) {
			zhi = 0;
		}
		etjj.setText(zhi + "");
	}

	// 自增运算,传入控件id
	public void addSelf(int id) {
		EditText etjj = (EditText) findViewById(id);
		String str = etjj.getText().toString().trim();
		int zhi = 0;
		if (!TextUtils.isEmpty(str)) {
			zhi = Integer.parseInt(str);
		} else {
			zhi = 0;
		}
		zhi = zhi + 1;
		etjj.setText(zhi + "");
	}
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值