reg activity
package com.example.myapplication;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class RegisterActivity extends AppCompatActivity {
private EditText edtUsername, edtPassword;
private Button btnRegister;
private void registerUser(String user, String pass) {
SharedPreferences pref = getSharedPreferences("AppPrefs", MODE_PRIVATE);
String jsonString = pref.getString("user_list", "[]");
try {
JSONArray jsonArray = new JSONArray(jsonString);
JSONObject newUser = new JSONObject();
newUser.put("username", user);
newUser.put("password", pass);
jsonArray.put(newUser);
pref.edit().putString("user_list", jsonArray.toString()).apply();
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_register);
edtUsername = findViewById(R.id.editRegUsername);
edtPassword = findViewById(R.id.editRegPassword);
btnRegister = findViewById(R.id.regButton2);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = edtUsername.getText().toString().trim();
String pass = edtPassword.getText().toString().trim();
if (user.isEmpty() || pass.isEmpty()) {
Toast.makeText(RegisterActivity.this, "Please enter all the information!", Toast.LENGTH_SHORT).show();
return;
}
registerUser(user, pass);
Toast.makeText(RegisterActivity.this, "Register success !", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
Editor is loading...
Leave a Comment