每日一笑:一领导下乡普查,问老汉:您知道近亲为什么不能结婚吗?l老汉憨厚的笑答:亲戚嘛,呵呵呵呵呵……呵呵,太熟,不好下手!
知识都是在于不断地积累,最近有点浮躁,但是很幸运,公司的一个同事点醒了我,非常感谢他的提醒!言归正传!上代码!
Demo下载地址:http://download.csdn.net/detail/qq_34148178/9687535
先来MainActivity里面的代码
package com.service.edittextmonitor;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
int f1 = 2;
int f2 = 2;
int f3 = 2;
int f4 = 2;
/**
* 作者:请叫我赵先森
*
* Emial:2691608900@qq.com
*/
/**
*
* 文档说明
*
* 额!先说下,我不是什么大神,我只是一个不断索取知识的小菜鸟,大神看见有合理的意见,可以指导一二,但请勿喷,谢谢!
*
* EditText.addTextChangedListener()方法是Android文本框改变的编辑器,这里用作监听EditText的实时状态的改变,在afterTextChanged方法中实现对汇总栏的数值改变
*
* MySwitch是一个自定义的转换开关,取材自网络,进行了小部分的修改!
*
* MySwitch.gethttp()方法是转换开关实时状态的监听,根据开关的当前装填来改变EditText的状态(也就是改变背景图片),当开关关闭时不能对对应的EditText进行操作
*
* 具体的逻辑就不讲了,写起来太坑了,看来还是技术水平有待提高!这个Demo耗费了我快一天的时间!
*
* 如果要对汇总栏进行操作,直接调用zmoney参数即可!
*/
private EditText ed_zh;
private EditText glf_dz;
private EditText ed_dz;
private EditText zs_dz;
private String ed_zhs;
private String glf_dzs;
private String ed_dzs;
private String zs_dzs;
private int sa;
private int zmoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里是你完成操作后将要进行的操作
}
});
findViewById(R.id.fanhui).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
ed_zh = (EditText) findViewById(R.id.ed_zh);
glf_dz = (EditText) findViewById(R.id.glf_dz);
ed_dz = (EditText) findViewById(R.id.ed_dz);
zs_dz = (EditText) findViewById(R.id.zs_dz);
zs_dz.setEnabled(false);
ed_zh.setEnabled(false);
glf_dz.setEnabled(false);
ed_dz.setEnabled(false);
ed_zhs = ed_zh.getText().toString();
glf_dzs = glf_dz.getText().toString();
ed_dzs = ed_dz.getText().toString();
zs_dzs = zs_dz.getText().toString();
ed_dz.addTextChangedListener(new TextWatcher() {
private int zs = 0;
private int o = 0;
private int z = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {//监听EditText中的数值,并进行操作
z = 0;
o = 0;
zs = 0;
ed_dzs = ed_dz.getText().toString();
Log.i("iiiii", s.toString());
if (!TextUtils.isEmpty(ed_dzs)) {
if (!TextUtils.isEmpty(s.toString())) {
zs = Integer.parseInt(s.toString());
glf_dzs = glf_dz.getText().toString();
if (!TextUtils.isEmpty(glf_dzs)) {
o = Integer.parseInt(glf_dzs);
}
ed_zhs = ed_zh.getText().toString();
if (!TextUtils.isEmpty(ed_zhs)) {
z = Integer.parseInt(ed_zhs);
}
zs_dz.setText(zs + o + z + "");
}
} else {
zs = 0;
Log.i("glf_dzs", glf_dzs + "空了");
zs = 0;
o = 0;
z = 0;
glf_dzs = glf_dz.getText().toString();
if (!TextUtils.isEmpty(glf_dzs)) {
z = Integer.parseInt(glf_dzs);
}
ed_zhs = ed_zh.getText().toString();
if (!TextUtils.isEmpty(ed_zhs)) {
o = Integer.parseInt(ed_zhs);
Log.i("zz", o + "");
}
if (zs == 0) {
if (o != 0 && z != 0) {
zs_dz.setText(o + z + "");
} else if (z != 0) {
zs_dz.setText(z + "");
} else if (o != 0) {
zs_dz.setText(o + "");
}else {
zs_dz.setText("");
}
}
}
zmoney = zs + o + z;
}
});
glf_dz.addTextChangedListener(new TextWatcher() {
private int o = 0;
private int z = 0;
private int zs = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("onTextChanged", count + "...." + s.toString());
}
@Override
public void afterTextChanged(Editable s) {
Log.i("zs", zs + "");
z = 0;
o = 0;
zs = 0;
glf_dzs = glf_dz.getText().toString();
Log.i("glf_dzs", glf_dzs + "空了");
if (!TextUtils.isEmpty(glf_dzs)) {
if (!TextUtils.isEmpty(s.toString())) {
zs = Integer.parseInt(s.toString());
ed_dzs = ed_dz.getText().toString();
if (!TextUtils.isEmpty(ed_dzs)) {
o = Integer.parseInt(ed_dzs);
}
ed_zhs = ed_zh.getText().toString();
if (!TextUtils.isEmpty(ed_zhs)) {
z = Integer.parseInt(ed_zhs);
}
zs_dz.setText(zs + o + z + "");
}
} else {
Log.i("glf_dzs", glf_dzs + "空了");
zs = 0;
o = 0;
z = 0;
ed_zhs = ed_zh.getText().toString();
if (!TextUtils.isEmpty(ed_zhs)) {
z = Integer.parseInt(ed_zhs);
}
ed_dzs = ed_dz.getText().toString();
if (!TextUtils.isEmpty(ed_dzs)) {
o = Integer.parseInt(ed_dzs);
Log.i("zz", o + "");
}
if (zs == 0) {
if (o != 0 && z != 0) {
zs_dz.setText(o + z + "");
} else if (z != 0) {
zs_dz.setText(z + "");
} else if (o != 0) {
zs_dz.setText(o + "");
}else {
zs_dz.setText("");
}
}
}
zmoney = zs + o + z;
}
});
ed_zh.addTextChangedListener(new TextWatcher() {
private int z = 0;
private int o = 0;
private int zs = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
z = 0;
o = 0;
zs = 0;
ed_zhs = ed_zh.getText().toString();
if (!TextUtils.isEmpty(ed_zhs)) {
if (!TextUtils.isEmpty(s.toString())) {
zs += Integer.parseInt(s.toString());
glf_dzs = glf_dz.getText().toString();
if (!TextUtils.isEmpty(glf_dzs)) {
o = Integer.parseInt(glf_dzs);
}
ed_dzs = ed_dz.getText().toString();
if (!TextUtils.isEmpty(ed_dzs)) {
z = Integer.parseInt(ed_dzs);
}
zs_dz.setText(zs + o + z + "");
}
} else {
zs = 0;
Log.i("glf_dzs", glf_dzs + "空了");
zs = 0;
o = 0;
z = 0;
glf_dzs = glf_dz.getText().toString();
if (!TextUtils.isEmpty(glf_dzs)) {
z = Integer.parseInt(glf_dzs);
}
ed_dzs = ed_dz.getText().toString();
if (!TextUtils.isEmpty(ed_dzs)) {
o = Integer.parseInt(ed_dzs);
Log.i("zz", o + "");
}
if (zs == 0) {
if (o != 0 && z != 0) {
zs_dz.setText(o + z + "");
} else if (z != 0) {
zs_dz.setText(z + "");
} else if (o != 0) {
zs_dz.setText(o + "");
}else {
zs_dz.setText("");
}
}
}
zmoney = zs + o + z;
}
});
MySwitch funSwitch2 = (MySwitch) findViewById(R.id.zh_swit);
funSwitch2.gethttp(new Inface() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void setinface() {
f3++;
if (f3 % 2 == 0) {
ed_zh.setEnabled(false);
ed_zh.setBackground(getResources().getDrawable(R.drawable.hs_bag));
if(f2%2==0&&f1%2==0){
zs_dz.setTextColor(0xcccccc);
ed_zh.setTextColor(0xcccccc);
zs_dz.setEnabled(false);
zs_dz.setBackground(getResources().getDrawable(R.drawable.hs_bag));
}
} else {
ed_zh.setEnabled(true);
ed_zh.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
zs_dz.setEnabled(true);
zs_dz.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
}
}
});
MySwitch funSwitch3 = (MySwitch) findViewById(R.id.glf_swit);
funSwitch3.gethttp(new Inface() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void setinface() {
Log.i("one", "3");
f2++;
if (f2 % 2 == 0) {
glf_dz.setEnabled(false);
glf_dz.setBackground(getResources().getDrawable(R.drawable.hs_bag));
if(f3%2==0&&f1%2==0){
zs_dz.setTextColor(0xcccccc);
glf_dz.setTextColor(0xcccccc);
zs_dz.setEnabled(false);
zs_dz.setBackground(getResources().getDrawable(R.drawable.hs_bag));
}
} else {
glf_dz.setEnabled(true);
glf_dz.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
zs_dz.setEnabled(true);
zs_dz.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
}
}
});
MySwitch funSwitch4 = (MySwitch) findViewById(R.id.swit);
funSwitch4.gethttp(new Inface() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void setinface() {
f1++;
if (f1 % 2 == 0) {
ed_dz.setEnabled(false);
ed_dz.setBackground(getResources().getDrawable(R.drawable.hs_bag));
if(f3%2==0&&f2%2==0){
//设置汇总栏的状态
zs_dz.setEnabled(false);
zs_dz.setBackground(getResources().getDrawable(R.drawable.hs_bag));
zs_dz.setTextColor(0xcccccc);
ed_dz.setTextColor(0xcccccc);
}
} else {
ed_dz.setEnabled(true);
ed_dz.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
zs_dz.setEnabled(true);
zs_dz.setBackground(getResources().getDrawable(R.drawable.blue_bg_zz));
}
Log.i("one", "4");
}
});
}
private void getConnect() {
}
}
相关xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue">
<ImageView
android:id="@+id/fanhui"
android:layout_width="25dp"
android:layout_marginLeft="6dp"
android:layout_height="25dp"
android:src="@drawable/fanhui"
android:layout_centerVertical="true"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/line_frames"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</RelativeLayout>
<RelativeLayout
android:background="@drawable/line_frames"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/dz"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="item1"
android:textSize="19sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/dz">
<EditText
android:id="@+id/ed_dz"
android:numeric="integer"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_weight="1"
android:textColor="@color/blue"
android:paddingLeft="25dp"
android:background="@drawable/hs_bag"
android:layout_gravity="center"
android:layout_toRightOf="@+id/dz"/>
<!-- <EditText
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_gravity="center"
android:background="@drawable/hse_frames"
android:layout_weight="1"
android:layout_toRightOf="@+id/dz"/>-->
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="/月"
android:textSize="19sp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/ed_dz"/>
<com.service.edittextmonitor.MySwitch
android:id="@+id/swit"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:background="@drawable/line_frames"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/glf"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="item1"
android:textSize="19sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/glf">
<EditText
android:numeric="integer"
android:paddingLeft="25dp"
android:background="@drawable/hs_bag"
android:id="@+id/glf_dz"
android:layout_width="match_parent"
android:layout_height="46dp"
android:textColor="@color/blue"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_toRightOf="@+id/dz"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="/月"
android:textSize="19sp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/ed_dz"/>
<com.service.edittextmonitor.MySwitch
android:id="@+id/glf_swit"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:background="@drawable/line_frames"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/zhf"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="item1"
android:textSize="19sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/zhf">
<EditText
android:textColor="@color/blue"
android:numeric="integer"
android:id="@+id/ed_zh"
android:paddingLeft="25dp"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_gravity="center"
android:background="@drawable/hs_bag"
android:layout_weight="1"
android:layout_toRightOf="@+id/dz"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="/月"
android:textSize="19sp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/ed_dz"/>
<com.service.edittextmonitor.MySwitch
android:id="@+id/zh_swit"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:checked="true"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:background="@drawable/line_frames"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/zs"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="汇总"
android:textSize="19sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/zs">
<EditText
android:paddingLeft="25dp"
android:id="@+id/zs_dz"
android:numeric="integer"
android:layout_width="match_parent"
android:layout_height="46dp"
android:textColor="@color/blue"
android:layout_gravity="center"
android:background="@drawable/hs_bag"
android:layout_weight="1"
android:layout_toRightOf="@+id/dz"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="/月"
android:textSize="19sp"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/ed_dz"
android:layout_marginRight="60dp"/>
</LinearLayout>
</RelativeLayout>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/home_org_btm"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="25dp"
android:text="保存"
android:textSize="19sp"/>
</LinearLayout>
此处只上传了部分主要代码,需要源码的请点击此处,移步下载源码!