用Android实现串扫描操作,将带有“111”字符的输出1,否则输出0
2015-10-09 09:47
441 查看
大体思路就是,先写出一个editext用来获取文本内容,再写出一个textview用来显示我们同步生成的文本。然后我们要调用系统的textWatcher的函数,然后会出现三个复习的子类:
@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) {
}这三个子类显然能看出,分别是在输入前,和输入中的,和输入后的函数,所以我们只需要在复写出的第三个函数中 写上我们的有关算法的代码即可,即:
@Override public void afterTextChanged(Editable s) {String result = "00";String str = editText.getText().toString();char[] b = new char[str.length()];for (int i = 0; i < str.length(); i++) {b[i] = str.charAt(i);}for (int j = 0; j < b.length - 2; j++) {if (Integer.parseInt(""+b[j])==1&&Integer.parseInt(""+b[j+1])==1&&Integer.parseInt(""+b[j+2])==1){result = result + "1";} else {result = result + "0";}}textView.setText(result);}这样我们便能实现我们想要得到的功能。//与此同时我们需要注意到的就是
String str = editText.getText().toString();//获取editext的文本内容
Integer.parseInt(""+b[j])==1 //当要做判断的时候应将数据类型统一
char[] b = new char[str.length()]; for (int i = 0; i < str.length(); i++) { b[i] = str.charAt(i); } //将一个string类型的数据一个一个的传入到char[]b的数组之中,从而获得一个数组类型全部代码如下:Mainactivity:
package com.example.lin_sir.scanning;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends AppCompatActivity {private EditText editText;private TextView textView;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText) findViewById(R.id.editText);textView = (TextView) findViewById(R.id.textview);editText.addTextChangedListener(textWatcher);}private TextWatcher textWatcher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {textView.setText("please input numbers");}@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }@Override public void afterTextChanged(Editable s) {String result = "00";String str = editText.getText().toString();char[] b = new char[str.length()];for (int i = 0; i < str.length(); i++) {b[i] = str.charAt(i);}for (int j = 0; j < b.length - 2; j++) {if (Integer.parseInt(""+b[j])==1&&Integer.parseInt(""+b[j+1])==1&&Integer.parseInt(""+b[j+2])==1){result = result + "1";} else {result = result + "0";}}textView.setText(result);}};}
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- PowerShell函数简明教程
- 探索PowerShell(十一)函数介绍