上两篇写完用户第一次启动欢迎界面的实现,那么欢迎界面结束后进入的应该就是用户登录界面,因为是菜鸟,就用最简单的方法去做了一个,当然还不支持注册等功能。
首先创建一个login_activity.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:background="@drawable/login"
android:orientation="vertical"
tools:context="com.linlif.cv.activity.MainActivity" >
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="310dp"
android:background="#ffffff"
android:hint="@string/username"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
<EditText
android:id="@+id/upwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="@string/userpwd"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
<CheckBox
android:id="@+id/chb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:checked="true"
android:text="@string/rememberpwd"
android:textColor="#ffffff" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:onClick="login"
android:text="@string/login"
android:textColor="#ffffff" />
</LinearLayout>
public class LoginActivity extends Activity {
private EditText uname;
private EditText upwd;
private CheckBox chb;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
//找到需要用到的控件
uname = (EditText) findViewById(R.id.uname);
upwd = (EditText) findViewById(R.id.upwd);
chb = (CheckBox) findViewById(R.id.chb);
//实例化SharedPreferences对象
sp = getSharedPreferences("info", 0);
//获取info文件中的name和pwd
String name = sp.getString("name", "");
String pwd = sp.getString("pwd", "");
//将info中的name和pwd显示在edittext中
uname.setText(name);
upwd.setText(pwd);
}
//登陆按钮的onclick属性为login,直接写login方法
public void login(View v) {
//获取edittext中的内容
String name = uname.getText().toString().trim();
String pwd = upwd.getText().toString().trim();
//判断其中一个是否为空
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
//其中一个为空则弹出一个toast,这里用到了自定义的ToastUtil,下一篇讲一下自定义Toast
ToastUtil.makeText(LoginActivity.this, R.drawable.toast_login, "不知道填用户名和密码?你傻啊!", Toast.LENGTH_SHORT).show();
} else {
//判断checkbox是否被选中
if (chb.isChecked()) {
//选中则传入数据
Editor edit = sp.edit();
edit.putString("name", name);
edit.putString("pwd", pwd);
edit.commit();
//进入下一个Activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else {
//没选中则弹出一个对话框
ToastUtil.makeText(LoginActivity.this, R.drawable.toast_pwd, "求求你勾选上吧", Toast.LENGTH_SHORT).show();
}
}
}
}
3,创建一个main_activity,布局以下登陆后的界面,结束,就是这么简单