一、安装 配置开发环境
安装模拟器
创建第一个项目
二、构建基本页面
基本控件的使用
1.TextView
2.Button
3.EditText
4.ImageView
...
页面间的跳转
点击注册按钮跳转到注册页面
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顶部导航栏的设置
activity_login.xml
<include layout="@layout/tool_bar" />
LoginActivity.java
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();
}
});