安卓登入的解決方案(記住密碼)
阿新 • • 發佈:2018-12-12
安卓登入解決方案
在安卓開發中,肯定少不了登入,前幾天寫了個登入的demo,就想著記錄下來,是新手,寫的可能不太好,但是能用,以後寫登入的時候直接copy不就好了麼哈哈哈哈哈O(∩_∩)O~
Emmm,先上程式碼吧
LoginActivity
public class LoginActivity extends Activity implements View.OnClickListener { private SharedPreferences sp; private SharedPreferences.Editor editor; private ProgressBar processBar; private AutoCompleteTextView autoUsername; private EditText editTextPassword; private Button btnSignIn; private Button btnSignUp; private CheckBox checkBox; public static int LOG_OUT = 0; //登出登入標識 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); sp = PreferenceManager.getDefaultSharedPreferences(this);//獲取SharedPreferences processBar =(ProgressBar) findViewById(R.id.login_progress); autoUsername = (AutoCompleteTextView) findViewById(R.id.username); editTextPassword = (EditText) findViewById(R.id.password); btnSignIn = (Button) findViewById(R.id.sign_in_button); btnSignUp = (Button) findViewById(R.id.sign_up_button); checkBox = (CheckBox)findViewById(R.id.rem_for_password); btnSignIn.setOnClickListener(this); btnSignUp.setOnClickListener(this); boolean isRemember = sp.getBoolean("remember_password",false); //在SharedPreferences中獲取是否記住密碼 if(getIntent().getFlags() == LOG_OUT){ //若是登出登入,那就不要記住密碼了是吧 isRemember = false; } if (isRemember){ //若記住密碼,則在SharedPreferences中獲取進行自動登入 String username = sp.getString("username",""); String password = sp.getString("password",""); autoUsername.setText(username); editTextPassword.setText(password); checkBox.setChecked(true); //記住密碼自動登入 doLogin(username,password); } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.sign_in_button: if(!processBar.isShown()){ processBar.setVisibility(View.VISIBLE); } String username = autoUsername.getText().toString(); String password = editTextPassword.getText().toString(); doLogin(username,password); break; case R.id.sign_up_button: Intent intent = new Intent(LoginActivity.this,SignUpActivity.class); startActivity(intent); break; default: break; } } //登入 private void doLogin(String username, String password) { editor = sp.edit(); if(checkBox.isChecked()){ //若勾選記住密碼,則在登入的時候將密碼儲存到SharedPreferences中 editor.putBoolean("remember_password",true); editor.putString("username",username); editor.putString("password",password); }else { editor.clear(); //否則清空 } editor.apply(); //登入請求(RetrofitFactory是自己封裝的網路請求工具,別到時候啥都複製不能用) RetrofitFactory.getInstance() .getService(UserService.class) .login(username, password) .enqueue(new Callback<SigninResponse>() { @Override public void onResponse(Call<SigninResponse> call, Response<SigninResponse> response) { String name = response.body().getResult().getName(); Toast.makeText(LoginActivity.this,"登入成功,歡迎"+name,Toast.LENGTH_SHORT).show(); MyApplication.setUser(response.body().getResult()); if(processBar.isShown()) processBar.setVisibility(View.GONE); Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); } @Override public void onFailure(Call<SigninResponse> call, Throwable t) { if(processBar.isShown()) processBar.setVisibility(View.GONE); Toast.makeText(LoginActivity.this,"登入失敗,請檢查您的賬戶或者密碼重新登入",Toast.LENGTH_SHORT).show(); } }); } }
activity_login.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:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <!-- Login progress --> <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:visibility="gone" /> <ScrollView android:id="@+id/login_form" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <AutoCompleteTextView android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_email" android:maxLines="1" android:singleLine="true" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_password" android:imeActionId="6" android:imeActionLabel="@string/action_sign_in_short" android:imeOptions="actionUnspecified" android:inputType="textPassword" android:maxLines="1" android:singleLine="true" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/sign_in_button" style="?android:textAppearanceSmall" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="16dp" android:text="@string/action_sign_in" android:textStyle="bold" /> <Button android:id="@+id/sign_up_button" style="?android:textAppearanceSmall" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="@string/action_sign_up" android:textStyle="bold" /> </LinearLayout> <CheckBox android:id="@+id/rem_for_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rem_or_for" /> </LinearLayout> </ScrollView> </LinearLayout>
登出登入
//登出登入 Button logOut = (Button) findViewById(R.id.btn_logout); logOut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(UerInfoActivity.this); SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("remember_password",false); editor.apply(); Intent intent = new Intent(UerInfoActivity.this,LoginActivity.class); intent.setFlags(LOG_OUT); startActivity(intent); finish(); } });
啊~都寫到這了,索性把註冊也撂這
SignUpActivity
public class SignUpActivity extends Activity {
private ProgressBar signupProgress;
private AutoCompleteTextView autoTextName;
private EditText editTextPassword;
private EditText editTextAge;
private EditText editTextAddress;
private Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
signupProgress =(ProgressBar) findViewById(R.id.sign_up_progress);
autoTextName = (AutoCompleteTextView) findViewById(R.id.sign_up_username);
editTextPassword = (EditText) findViewById(R.id.sign_up_password);
editTextAge = (EditText) findViewById(R.id.sign_up_age);
editTextAddress = (EditText)findViewById(R.id.sign_up_address);
btnSignUp = (Button) findViewById(R.id.sign_up);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!signupProgress.isShown()){
signupProgress.setVisibility(View.VISIBLE);
}
doSignUp();
}
});
}
/**
* 註冊
*/
public void doSignUp() {
String username = autoTextName.getText().toString();
String password = editTextPassword.getText().toString();
String age = editTextAge.getText().toString();
String address = editTextAddress.getText().toString();
RetrofitFactory.getInstance()
.getService(UserService.class)
.toSign(username,password, Integer.parseInt(age),address)
.enqueue(new Callback<SignUpResponse>() {
@Override
public void onResponse(Call<SignUpResponse> call, Response<SignUpResponse> response) {
Toast.makeText(SignUpActivity.this,"註冊成功,去登入吧",Toast.LENGTH_SHORT).show();
if(signupProgress.isShown())
signupProgress.setVisibility(View.GONE);
//註冊成功後跳轉到登入介面
Intent intent = new Intent(SignUpActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
@Override
public void onFailure(Call<SignUpResponse> call, Throwable t) {
if(signupProgress.isShown())
signupProgress.setVisibility(View.GONE);
Toast.makeText(SignUpActivity.this,"註冊失敗",Toast.LENGTH_SHORT).show();
}
});
}
}
activity_signup
<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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<!-- Login progress -->
<ProgressBar
android:id="@+id/sign_up_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="@+id/sign_up_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/sign_up_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_email"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/sign_up_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="6"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/sign_up_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_age"
android:imeActionId="6"
android:inputType="number"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="@+id/sign_up_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_address"
android:imeActionId="6"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="@+id/sign_up"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_up"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
刺激刺激~寫完就走