如图,使用两个按钮控制九个输入框实现自增自减。从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 + "");
}
}