读取数据
读取数据
1.首先界面布局,做成简单的登陆界面就可以
2.实现功能
界面代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.administrator.denglujiemian.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="60px"
android:textColor="#FF0000" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"
android:textSize="30px" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="60px"
android:textColor="#FF0000" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"
android:textSize="30px"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写 入"
android:textSize="30px" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读 取"
android:textSize="30px" />
</LinearLayout>
</LinearLayout>
功能实现代码:
package com.example.administrator.denglujiemian;
public class MainActivity extends AppCompatActivity {
private EditText eveditText1;
private EditText eveditText2;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eveditText1 = (EditText) findViewById(R.id.editText1);
eveditText2 = (EditText) findViewById(R.id.editText2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String editText1 = eveditText1.getText().toString();
String editText2 = eveditText2.getText().toString();
Toast.makeText(MainActivity.this, "用户名:" + editText1 +
"密码" + editText2, Toast.LENGTH_LONG).show();
if (saveToFile(editText1)) {
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
}
}
});
button1 = (Button) findViewById(R.id.button1);
eveditText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String editText1 = eveditText1.getText().toString();
String filename = "data.txt";
String result = "";
try {
FileInputStream in = openFileInput(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
editText1 = reader.readLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
editText1 = eveditText1.getText().toString();
String editText2 = eveditText2.getText().toString();
Toast.makeText(MainActivity.this, "用户名:" + editText1 + ",密码:" + editText2, Toast.LENGTH_SHORT).show();
}
});
}
private boolean saveToFile(String Name) {
try {
FileOutputStream out = openFileOutput("data.txt", MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(Name);
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
效果

浙公网安备 33010602011771号