Android 开发(二)登陆界面
1、使用MyEclipse创建Android工程DianDianMi,其中应用名为DianDianMi,工程名为DianDianMi,包名为com.diandianmi.activity。如下图:
Minimum Required SDK:选择最低支持的SDK,这里选择2.2版,太高了就不支持低版本的了。按照自己的需求选择一下几个选项。
一直点Next,直到出现创建New Black Activity,Activity Name为LoginActivity,Layout Name为activity_login,点击Finish按钮,创建完成。
2、打开res->layout->activity_login.xml布局文件,编写以下代码:
1 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 > 7 <TableRow> 8 <TextView 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/account" 12 android:textSize="10pt" 13 /> 14 15 <EditText 16 android:id="@+id/user" 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:ems="10" 20 android:hint="@string/message" 21 android:selectAllOnFocus="true" > 22 23 <requestFocus /> 24 </EditText> 25 26 </TableRow> 27 <TableRow> 28 <TextView 29 android:layout_width="fill_parent" 30 android:layout_height="wrap_content" 31 android:text="@string/password" 32 android:textSize="10pt" 33 /> 34 <EditText 35 android:id="@+id/password" 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:inputType="textPassword" 39 /> 40 </TableRow> 41 <TableRow > 42 <CheckBox 43 android:id="@+id/rememberpassword" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="@string/rememberpassword"/> 47 <CheckBox 48 android:id="@+id/autologin" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="@string/aulog"/> 52 </TableRow> 53 <Button 54 android:id="@+id/login" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:text="@string/login"/> 58 <Button 59 android:id="@+id/register" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:text="@string/register"/> 63 64 </TableLayout>
运行界面如下图:
在类LoginActivity中的代码如下图:
1 package com.diandianmi.activity; 2 3 import com.diandianmi.service.DataBaseService; 4 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.content.SharedPreferences; 9 import android.view.Menu; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.CheckBox; 14 import android.widget.CompoundButton; 15 import android.widget.CompoundButton.OnCheckedChangeListener; 16 import android.widget.EditText; 17 import android.widget.Toast; 18 19 public class LoginActivity extends Activity { 20 21 22 //SharedPreferences.Editor editor; 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_login); 27 28 final EditText user = (EditText)findViewById(R.id.user);//获取activity_login的用户输入框 29 final EditText password = (EditText)findViewById(R.id.password);//获取activity_login的密码输入框 30 final CheckBox remPsd = (CheckBox)findViewById(R.id.rememberpassword); 31 CheckBox autoLogin = (CheckBox)findViewById(R.id.autologin); 32 33 final DataBaseService dbSer = new DataBaseService(this);//数据库操作 34 SharedPreferences preferences = getSharedPreferences("diandianmi",MODE_PRIVATE);//记录一些信息,使用SharePreferences更方便。 35 final SharedPreferences.Editor editor = preferences.edit();//对SharePreferences进行操作。 36 37 String userName = preferences.getString("userName", null);//获取保存过的用户名。 38 Boolean isRemPsd = preferences.getBoolean("rememberpassword", false);//获取是否记住密码 39 Boolean isAutoLogin = preferences.getBoolean("autologin", false);//获取是否自动登录 40 if(userName!=null&&isRemPsd&&isAutoLogin){ 41 //如果自动登录,并且记住用户名,就直接进入到MainActivity 42 Intent intent = new Intent(LoginActivity.this,MainActivity.class); 43 44 startActivity(intent); 45 }else if(isRemPsd&&userName!=null){ 46 //如果只是记住密码,则在用户框中输入用户名,密码框中输入密码。 47 user.setText(userName); 48 String psd = preferences.getString("password", null); 49 password.setText(psd); 50 } 51 Button button = (Button)findViewById(R.id.login); 52 button.setOnClickListener(new OnClickListener(){ //点击登录按钮产生点击事件 53 54 @Override 55 public void onClick(View arg0) { 56 57 Boolean isHaveUser = dbSer.haveUser(user.getText().toString(), password.getText().toString());//查询数据库中用户 和密码是否正确 58 if(isHaveUser){ 59 editor.putString("userName", user.getText().toString()); 60 editor.putString("password", password.getText().toString()); 61 62 Intent intent = new Intent(LoginActivity.this,MainActivity.class); 63 startActivity(intent); 64 65 finish(); 66 }else{ 67 Toast.makeText(LoginActivity.this, "user name or password error" , Toast.LENGTH_LONG).show(); 68 } 69 70 71 } 72 73 }); 74 Button registerBtn = (Button)findViewById(R.id.register); 75 registerBtn.setOnClickListener(new OnClickListener(){//点击注册按钮,进行注册。 76 77 @Override 78 public void onClick(View v) { 79 80 Intent intent = new Intent(LoginActivity.this,RegisterActivity.class); 81 startActivity(intent); 82 83 finish(); 84 } 85 86 }); 87 88 remPsd.setOnCheckedChangeListener(new OnCheckedChangeListener(){//点击记住密码复选框 89 90 @Override 91 public void onCheckedChanged(CompoundButton buttonView, 92 boolean isChecked) { 93 // TODO Auto-generated method stub 94 editor.putBoolean("rememberpassword", isChecked); 95 } 96 97 }); 98 autoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener(){//点击自动登录复选框 99 100 @Override 101 public void onCheckedChanged(CompoundButton buttonView, 102 boolean isChecked) { 103 // TODO Auto-generated method stub 104 editor.putBoolean("autologin", isChecked); 105 if(isChecked){ 106 //如果自动登录,记住密码也要选上。 107 remPsd.setChecked(isChecked); 108 editor.putBoolean("rememberpassword", isChecked); 109 } 110 } 111 112 }); 113 } 114 115 @Override 116 public boolean onCreateOptionsMenu(Menu menu) { 117 // Inflate the menu; this adds items to the action bar if it is present. 118 getMenuInflater().inflate(R.menu.login, menu); 119 return true; 120 } 121 122 } 123
在这里使用到了EditView,CheckBox控件,SharePreference存储记录,切换到其他Activity使用Intent,还有数据库操作。下面讲解数据库。