暂时还不是特别清楚程序结构,代码先贴在这里。
main.xml
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/edtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt3"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/edtPW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt2"
android:layout_marginTop="28dp"
android:ems="10" />
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edtID"
android:layout_marginTop="14dp"
android:text="输入密码" />
<EditText
android:id="@+id/edtID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt1"
android:layout_marginTop="34dp"
android:ems="10" />
<Button
android:id="@+id/btnAppend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加入数据"
android:layout_below="@id/edtPW"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/edtPW"
android:text="结束" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt3"
android:layout_centerHorizontal="true"
android:text="清除数据" />
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="输入账号" />
<TextView
android:id="@+id/txt3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnAppend"
android:layout_marginTop="22dp"
android:text="文件内容" />
</RelativeLayout>
Java代码
package com.Harris.inputdataactivity;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InputDataActivity extends Activity {
private EditText edtID;
private EditText edtPW;
private EditText edtContent;
private Button btnAppend;
private Button btnClear;
private Button btnEnd;
private static final String FILENAME = "Login.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtID = (EditText) findViewById(R.id.edtID);
edtPW = (EditText) findViewById(R.id.edtPW);
edtContent = (EditText) findViewById(R.id.edtContent);
btnAppend = (Button) findViewById(R.id.btnAppend);
btnClear = (Button) findViewById(R.id.btnClear);
btnEnd = (Button) findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(listener);
btnClear.setOnClickListener(listener);
btnAppend.setOnClickListener(listener);
DisplayFile(FILENAME);
}
private void DisplayFile(String fname){
try {
FileInputStream fin;
BufferedInputStream buffin;
fin = openFileInput(fname);
buffin = new BufferedInputStream(fin);
byte[] buffbyte = new byte[20];
edtContent.setText("");//读取数据,直到文件结尾
do {
int flag= buffin.read(buffbyte);
if(flag==-1) break;
else
edtContent.append(new String(buffbyte), 0, flag);//显示数据
}while(true); buffin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Button.OnClickListener listener = new Button.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAppend:
if(edtID.getText().toString().equals("")||edtPW.getText().toString().equals("")){
Toast.makeText(InputDataActivity.this, "账号及密码都必须输入", Toast.LENGTH_LONG).show();
break;
}
FileOutputStream fout = null;
BufferedOutputStream buffout =null;
try {
fout = openFileOutput(FILENAME,MODE_APPEND);
buffout = new BufferedOutputStream(fout);
//写入账号密码
buffout.write(edtID.getText().toString().getBytes());
buffout.write("\n".getBytes());
buffout.write(edtPW.getText().toString().getBytes());
buffout.write("\n".getBytes());
buffout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DisplayFile(FILENAME);
edtID.setText("");
edtPW.setText("");
break;
case R.id.btnClear://清除数据
//以重载方式打开文件
try {
fout = openFileOutput(FILENAME, MODE_PRIVATE);
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DisplayFile(FILENAME);
break;
case R.id.btnEnd://结束
finish();
default:
break;
}
}
};
}
完成输入之后会在File Explorer中的<data/data/coms.Harris.InputDataActivity/files>下面看到一个Login.txt文件,导出就可以查看了。
还是java的IO流没有学好啊。
bug1:模拟器里输入不了汉字,只能输入字母和数字,我改成字符流都不行为什么?
下面接着改,用来判断输入的账户名和密码是否匹配一个已有的文件里面的账户密码
基本思路是:
1.在onCreate方法里面提取出login.txt文件的内容到一个String[] login 数组里面。奇数位代表账号,偶数位元素代表密码。后来打印出来没错。
2.在Button上面添加一个OnClickListener()
判断的步骤:
先判断账户密码输入是否为空,为空的话Toast一个消息,break出来;
定义一个boolean类型的flag参数,初始值为false,用于判断账号输入是否正确。同时将flag的值设置为true。
接下来开始判断,如果账号正确,AlertDialog提示登陆成功。
账户存在但密码错误以Toast形式展示出来。
如果账户本身即为错误if(!flag),Toast提示账户错误。
onClick方法的复写主要就是这些,但是碰到了一个问题:
09-03 04:04:44.738: I/Choreographer(9639): Skipped 58 frames! The application may be doing too much work on its main thread.
在onCreate方法里面写了太多,网上查了下应该需要多线程来解决,这个暂时我无能为力。
先把代码贴在这里吧。
main.xml
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/edtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt3"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/edtPW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt2"
android:layout_marginTop="28dp"
android:ems="10"
android:password="true"/>
<TextView
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edtID"
android:layout_marginTop="14dp"
android:text="输入密码" />
<EditText
android:id="@+id/edtID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txt1"
android:layout_marginTop="34dp"
android:ems="10" />
<Button
android:id="@+id/btnAppend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定登陆"
android:layout_below="@id/edtPW"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/btnEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/edtPW"
android:text="结束" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt3"
android:layout_centerHorizontal="true"
android:text="清除数据" />
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="输入账号" />
<TextView
android:id="@+id/txt3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnAppend"
android:layout_marginTop="22dp"
android:text="文件内容" />
</RelativeLayout>
Java代码
package com.Harris.inputdataactivity;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class InputDataActivity extends Activity {
private EditText edtID;
private EditText edtPW;
private EditText edtContent;
private Button btnAppend;
private Button btnClear;
private Button btnEnd;
private static final String FILENAME = "Login.txt";//默认在默认位置创建一个文件,名字你说了算
private String[] login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtID = (EditText) findViewById(R.id.edtID);
edtPW = (EditText) findViewById(R.id.edtPW);
edtContent = (EditText) findViewById(R.id.edtContent);
btnAppend = (Button) findViewById(R.id.btnAppend);
btnClear = (Button) findViewById(R.id.btnClear);
btnEnd = (Button) findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(listener);
btnClear.setOnClickListener(listener);
btnAppend.setOnClickListener(listener);
Distill(FILENAME);//该方法用于把login。txt里面预先存储的账号密码提取到一个login[]的String类型数组里面
//需要的参数只有FILENAME,其实也就是Login这个文件袋额位置了。
//我这里把一大段代码提取到CheckIn里面,为了面向对象而面向对象
}
private void Distill(String filename){
String fdata ="";
FileInputStream fis =null;
BufferedInputStream bis = null;
try {
fis = openFileInput(FILENAME);
bis = new BufferedInputStream(fis);
byte[] buffbyte = new byte[20];
do {
int flag= bis.read(buffbyte);
if(flag==-1)break;
else fdata+=new String(buffbyte);
} while (true);
bis.close();
} catch (IOException e) {
e.printStackTrace();
}login = fdata.split("\r\n");
System.out.println("下面看下login数组是否提取成功");
for(int i=0;i<login.length;i++){
System.out.println(login[i]);
}
}
//教材上为什么这么喜欢用生成一个内部类的方法,匿名内部类肯定不行,这么长,并列外部类需要拿到主Activity的成员变量,太麻烦。
private Button.OnClickListener listener = new Button.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAppend:
if(edtID.getText().toString().equals("")||edtPW.getText().toString().equals("")){
new AlertDialog.Builder(getApplicationContext()).setTitle("我是Title").setMessage("账号密码都得输入!")
.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
}
}).show();
//注意这个break;
break;
}
System.out.println("判断EditText里面已近不是空值了");
Boolean flag=false;
for(int i=0;i<login.length;i+=2){
System.out.println("for循环开始执行了");
if (edtID.getText().toString().equals(login[i])){//这说明账户存在
flag = true;
{System.out.println("flag的值已近被设置为true");
if(edtPW.getText().toString().equals(login[i+1])){
System.out.println("1111111");
new AlertDialog.Builder(getApplicationContext()).setTitle("这是账户密码验证全部成功的Title").setMessage("连密码也对了")
.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int i) {finish();}
}).show();
}else{
System.out.println("22222222");
Toast.makeText(getApplicationContext(),"账户对密码错",Toast.LENGTH_LONG).show();
//下面这两句完全可以提取成一个函数嘛,手懒不弄了
edtID.setText("");
edtPW.setText("");
break;
}
}
if(!flag){
System.out.println("!flag语句被执行");
/*new AlertDialog.Builder(getApplicationContext()).setTitle("这是账户都没过关的Title").setMessage("账户错误")
.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int i) {}
}).show(); */
Toast.makeText(getApplicationContext(),"一直执行这段话",Toast.LENGTH_LONG).show();
edtID.setText("");
edtPW.setText("");
}
}
else
System.out.println("I/Choreographer(9639): Skipped 58 frames! The application may be doing too much work on its main thread.");
}
System.out.println("for循环执行完毕");
break;
case R.id.btnClear:
edtID.setText("");
edtPW.setText("");
break;
case R.id.btnEnd:
finish();
break;
}
}
};
}
一个下午基本上都浪费在这个逻辑判断里面了,实在是不应该啊。
一方面是逻辑能力确实一坨屎,但目前的重点在于上手。