Android Studio的下載和安裝
下載和安裝
Android Support v4\v7\v13和AndroidX的區(qū)別及應用場景
區(qū)別及應用場景
項目的實踐
問候歡迎界面
跳轉界面
點擊注冊按鈕跳轉到注冊界面
Button tvRegister = findViewById(R.id.register_login);
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
Toolbar頂部導航欄的設置
<include layout="@layout/tool_bar" />
1
登錄界面的頂部導航欄
private void initToolbar(){
toolbar = findViewById(R.id.title_toolbar);
toolbar.setTitle("登錄");
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);//設置返回鍵
// actionBar.setHomeButtonEnabled(true);//設置是否是首頁
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginActivity.this.finish();
}
});
}
登錄注冊功能
驗證用戶名和密碼,正確后跳轉到我的界面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initToolbar();
initView();
initData();
btnLogin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String username=etUsername.getText().toString();
String password=etPassword.getText().toString();
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String name = pref.getString("username", "");
String pwd = pref.getString("password", "");
if (TextUtils.isEmpty(username)) {
Toast.makeText(LoginActivity.this, "用戶名不能為空", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "密碼不能為空", Toast.LENGTH_SHORT).show();
// } else if (!username.equals(name)) {
// Toast.makeText(LoginActivity.this, "用戶名錯誤"+username+","+name , Toast.LENGTH_SHORT).show();
} else if (!MD5Utils.md5(password).equals(pwd)) {
Toast.makeText(LoginActivity.this, "密碼錯誤", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
saveLoginStatus(username,true);
//返回我的界面
Intent intent = new Intent();
intent.putExtra("isLogin" , true);
intent.putExtra("longinUser",username);
setResult(RESULT_OK,intent);
LoginActivity.this.finish();
}
}
});
}
底部導航欄
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/main_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
</LinearLayout>
<RadioGroup
android:id="@+id/btn_group"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#F2F2F2"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btn_course"
style="@style/TabMenuItem"
android:drawableTop="@drawable/selector_nav_course"
android:text="課程" />
<RadioButton
android:id="@+id/btn_execise"
style="@style/TabMenuItem"
android:drawableTop="@drawable/selector_nav_execise"
android:text="習題" />
<RadioButton
android:id="@+id/btn_message"
style="@style/TabMenuItem"
android:drawableTop="@drawable/selector_nav_message"
android:text="資訊" />
<RadioButton
android:id="@+id/btn_my"
style="@style/TabMenuItem"
android:drawableTop="@drawable/selector_nav_my"
android:checked="true"
android:text="我" />
</RadioGroup>
</RelativeLayout>
登出功能
// 退出登錄
exitLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SettingActivity.this, "退出登錄", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(SettingActivity.this)
.setTitle("退出")
.setMessage("確認退出登錄闹丐?")
.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedUtils.clearLoginInfo(SettingActivity.this);
// 返回我的界面
Intent intent = new Intent();
intent.putExtra("isLogin", false);
setResult(RESULT_OK, intent);
SettingActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
});