首页 > 解决方案 > 使用资产文件夹中的文本文件匹配并重定向到下一个活动

问题描述

这是我使用的函数的代码:

 public void redirecttohome() {
            //variable init
            edtpass = (EditText) findViewById(R.id.edtpass);
            edtuserid = (EditText) findViewById(R.id.edtuserid);
            String text="";   
    try {
       InputStream is = getAssets().open("login.txt");
       int size = is.available();
       byte [] buffer = new byte[size];
       is.read(buffer);
       text = new String (buffer);          
       if (edtuserid.getText().toString().equals(text) && edtpass.getText().toString().equals(text)) {
          startActivity(new Intent(getApplicationContext(), MainActivity.class));
      }
     else {Snackbar.make(parent_view, "Invalid UserID and Password", Snackbar.LENGTH_SHORT).show();}
          is.close();
    } catch (IOException e) {
       Log.e("Error!", "Error occured while reading text file from Internal Storage!");
    }
 }

我想从保存为资产目录的文本中检查用户 ID 和密码并重定向到家庭活动。

标签: javaandroidandroid-studio

解决方案


您正在检查从文件“login.txt”中获取的字符串是否与输入到您的 editTexts 的用户 ID 和密码相等。例如,login.txt 有类似“user1,password”的内容,并且您在 edittext 中输入了“user1”作为 userId,在 edittext 中输入了“password”作为密码。检查文件中每个编辑文本和信息的内容是否相等将失败。“用户1,密码”!=“用户1”&&“用户1,密码”!=“密码”。您需要在“login.txt”文本的逗号符号之前仅检查 userId。与密码相同:检查密码(在逗号符号和空格之后)。


推荐阅读