内部存储文件(写)
為了后續(xù)項(xiàng)目使用,先把文件寫的代碼貼在這里,備用:
?
activity_main.xml代碼:
?
<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.swust.intern.MainActivity" ><!-- ~~~~~~~~~~~~~~~~~~~~~ 第一行 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --><LinearLayout android:layout_width="match_parent"android:layout_height="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="賬 號:" /><EditText android:hint="輸入用戶名"android:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout><!-- ~~~~~~~~~~~~~~~~~~~~~ 第二行密碼是 密文顯示 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 碼:" /><EditText android:hint="輸入密碼"android:id="@+id/et_pwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="textPassword"/></LinearLayout><!-- ~~~~~~~~~~~~~~~~~~~~~ 第三行:相對布局 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --><!-- 復(fù)選框是相對于RelativeLayout的垂直居中 --><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="記住用戶名和密碼" android:id="@+id/cb"android:layout_centerVertical="true"/><Button android:text="登 陸"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:layout_alignParentRight="true"android:onClick="login"/></RelativeLayout> </LinearLayout>后臺邏輯代碼:
MainActivity.java代碼:
package com.swust.intern;import java.io.File; import java.io.FileOutputStream;import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void login(View v){EditText et_name = (EditText) findViewById(R.id.et_name);EditText et_pwd = (EditText) findViewById(R.id.et_pwd);CheckBox cb = (CheckBox)findViewById(R.id.cb);String name=et_name.getText().toString();String pwd =et_pwd.getText().toString();//判讀復(fù)選框是否被勾選if(cb.isChecked()){//"data/data/com.swust.intern"是內(nèi)部存儲空間路徑File file =new File("data/data/com.swust.intern/info.txt");FileOutputStream fos;try{fos = new FileOutputStream(file);//加“##”是為了讀取時好分割fos.write((name + "##" +pwd).getBytes());fos.close();}catch (Exception e){e.printStackTrace();}}//System.out.println("登陸成功");//創(chuàng)建吐司對話框/* 第一個參數(shù)上context,而activity本來就是context的子類,所以直接填this*//*第三個參數(shù)為持續(xù)顯示時間,只有LENGTH_SHORT(2S)和LENGTH_LONG(5s)兩種選擇*//*Toast t= Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT);//顯示吐司對話框t.show();*/Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show();} }效果是在這里查看并導(dǎo)出:
轉(zhuǎn)載于:https://www.cnblogs.com/shuqingstudy/p/4876662.html
總結(jié)
- 上一篇: 【Android】Theme.AppCo
- 下一篇: Java StringBuffer类