下面是具體代碼,其中MainActivity.java的部分代碼有修改,在文章后面給出
logindemo_layout.java
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/background1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:padding="30dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_marginLeft="30dp" android:drawableLeft="@mipmap/ic_launcher_round" android:text="家庭記賬本" android:textSize="40sp" android:layout_height="wrap_content"/> <EditText android:layout_width="match_parent" android:layout_marginTop="30dp" android:id="@+id/et_username" android:layout_height="wrap_content" android:hint="用戶名" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_password" android:hint="密碼" /> <Button android:layout_width="match_parent" android:text="登錄" android:textSize="20sp" android:id="@+id/bt_login" android:layout_height="wrap_content"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="沒有賬號,立即去注冊" android:textColor="#00ffff" android:textSize="16sp" /> </RelativeLayout> </LinearLayout> </RelativeLayout>
MainActivity.java
package com.example.logindemo; import androidx.appcompat.app.AppCompatActivity; import android.nfc.Tag; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { private static final String TAG ="MainActivity"; private TextView mUsername; private TextView mPassword; private Button mLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logindemo_layout); //第一步,找到控件 initViews(); //第二步,給我們的登錄按鈕設置監聽事件 initListener(); } /** * 這個方法,我們用來找對應的控件 */ private void initViews(){ mUsername= (TextView)this.findViewById(R.id.et_username); mPassword= (TextView)this.findViewById(R.id.et_password); mLogin = (Button)this.findViewById(R.id.bt_login); } /** * 這個方法就是給登錄按鈕設置點擊的監聽 */ private void initListener(){ mLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG,"點擊了登錄按鈕"); handlerLoginEvent(v); } }); } /** * 處理登錄事件 * @param v */ private void handlerLoginEvent(View v) { //第三部,我們要拿到界面上的信息,包括賬號和密碼 //賬號 String usernameText =mUsername.getText().toString(); //密碼 String passwordText =mPassword.getText().toString(); //把賬號和密碼保存起來 saveUserInfo(usernameText,passwordText); } private void saveUserInfo(String usernameText,String passwordText){ Log.d(TAG,"保存用戶信息"); File file =new File("info.txt"); try { FileOutputStream fileOutputStream = new FileOutputStream(file); //以特定的格式存儲 fileOutputStream.write((usernameText+"***"+passwordText).getBytes()); fileOutputStream.close(); }catch (Exception e){ e.printStackTrace(); } } }
點擊登錄后,發現程序崩了
為什么我們直接寫一個文件名的時候,去寫文件,報出的異常是read-only。
在安卓系統中,每一個應用就相當於一個用戶,每個用戶(應用)的權限是特定的,不能夠操作其它應用的內容
找到我們的項目
查看我們的info文件應該存放的目錄
修改其中saveUserInfo方法
private void saveUserInfo(String usernameText,String passwordText){ Log.d(TAG,"保存用戶信息"); try { File file =new File("/data/data/com.example.logindemo/info.txt"); if(file.exists()){ file.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(file); //以特定的格式存儲 fileOutputStream.write((usernameText+"***"+passwordText).getBytes()); fileOutputStream.close(); }catch (Exception e){ e.printStackTrace(); } }
點擊登錄
查看發現info.txt文件,且內容為zzw***123說明保存成功。
還有另一種方法
通過Android Devices Monitor查看
https://www.cnblogs.com/rivers-enduring/p/9212111.html
打開后,在File Explorer下找到包/data/data/com.example.logindemo/
選中info.txt,右上角有導出
導出到桌面,進行查看