Android Walker登录记住密码页面功能实现
人气:0目标效果:
这一次修改的不多,添加了点击用户登录的跳转,登录页面的记住密码,和程序运行一次后,不进入导航页面的功能。
1.MainActivity.java页面修改了setOnItemClickListener的点击事件,进行跳转。
MainActivity.java页面:
package com.example.login; import java.util.ArrayList; import java.util.List; import com.example.walkersimulate.util.SlideMenu; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private SlideMenu slideMenu; private ImageView ivSwitchSlideMenu; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initMenuList(); slideMenu = (SlideMenu) findViewById(R.id.slideMenu); ivSwitchSlideMenu = (ImageView) findViewById(R.id.switch_slidemenu); ivSwitchSlideMenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (slideMenu.isMainScreenShowing()) {// 判断滑动菜单是否已打开,如果未打开 slideMenu.openMenu(); // 打开滑动菜单 } else { slideMenu.closeMenu();// 关闭滑动菜单 } } }); } private void initMenuList() { /*设置数据源(图片和文本信息)*/ int[] icons = { R.drawable.icons_menu_login, R.drawable.icons_menu_sport, R.drawable.icons_menu_inform, R.drawable.icons_menu_history, R.drawable.icons_menu_weather, R.drawable.icons_menu_health, R.drawable.icons_menu_setting }; final String[] introductons = getResources().getStringArray( R.array.menulist); /*实例列表*/ List<Item> items = new ArrayList<Item>(); /*向列表中添加图片和对应的文本信息*/ for (int i = 0; i < icons.length; i++) { items.add(new Item(icons[i], introductons[i])); } ListView lvMenuList = (ListView) findViewById(R.id.lvMenuList); /*创建适配器*/ itemAdapter adapter = new itemAdapter(this, R.layout.menulist_item, items); /*配置适配器*/ lvMenuList.setAdapter(adapter); /*列表某一项的点击事件*/ lvMenuList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { switch (position) { case 0: intent=new Intent(MainActivity.this,LoginActivity.class); startActivity(intent); break; default: break; } } }); } }
2.WelcomeActivity.java页面修改onAnimationEnd动画结束事件。
WelcomeActivity.java页面:
package com.example.login; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.RelativeLayout; public class WelcomeActivity extends Activity { private Intent intent; private SharedPreferences pref; private boolean isFirst=true;//用于判断是否是第一次运行,运行后变为false @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); RelativeLayout layoutWelcome=(RelativeLayout) findViewById(R.id.layoutwelcome); AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f); alphaAnimation.setDuration(3000); layoutWelcome.startAnimation(alphaAnimation); alphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { judgeIntent(); } private void judgeIntent() { pref=getSharedPreferences("isFirst",MODE_PRIVATE);//创建SharedPreferences对象 isFirst=pref.getBoolean("isFirstIn",true);//如果第一次运行,无isFirstIn值,自动获取第二个参数为默认值 if(isFirst){//如果为true,进入if语句 intent=new Intent(WelcomeActivity.this,GiudeActivity.class); Editor editor=pref.edit(); editor.putBoolean("isFirstIn",false);//保存isFirstIn值为false editor.commit();//提交数据 }else{ intent=new Intent(WelcomeActivity.this,MainActivity.class);//如果为false,说明程序已经运行过,直接跳转到主页面 } startActivity(intent); finish(); } }); } }
3.login_top.xml页面添加多选框,用于选择记住密码。
login_top.xml页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/logintop_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_top_margin" tools:context=".MainActivity" > <EditText android:id="@+id/etName" android:hint="@string/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/etaccount" android:drawablePadding="25dp" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/etPass" android:hint="@string/etPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etName" android:layout_below="@+id/etName" android:layout_marginTop="10dp" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/etpass" android:drawablePadding="10dp" android:ems="10" android:inputType="textPassword" /> <CheckBox android:id="@+id/cbSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="110dp" android:text="记住密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etPass" android:layout_below="@+id/etPass" android:layout_marginTop="40dp" > <Button android:id="@+id/btLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btchange_bg" android:text="@string/btLogin" /> <Button android:id="@+id/btRegister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btchange_bg" android:text="@string/btRegister" android:layout_marginLeft="5dp" /> </LinearLayout> </RelativeLayout>
4.LoginActivity.java页面处理数据并保存。
LoginActivity.java页面:
package com.example.login; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity implements OnClickListener{ private EditText etName,etPass; private CheckBox cbSave; private Button btLogin,btRegister; private SharedPreferences pref; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); /*获取控件id*/ getId(); /*获取数据*/ getData(); /*绑定点击事件*/ bindClick(); } /*获取控件id*/ private void getId() { etName=(EditText) findViewById(R.id.etName); etPass=(EditText) findViewById(R.id.etPass); cbSave=(CheckBox) findViewById(R.id.cbSave); btLogin=(Button) findViewById(R.id.btLogin); btRegister=(Button) findViewById(R.id.btRegister); } /*获取数据*/ private void getData() { pref=getSharedPreferences("savaLogin",MODE_PRIVATE);//创建SharedPreferences对象 editor=pref.edit();//创建SharedPreferences的编辑器 String userName=pref.getString("userName","");//获取用户名,没有则用空代替 String userPass=pref.getString("userPass",""); if(userName.equals("")){//如果为空,代表前一次为选择记住密码,则这次显示记住密码多选框不打勾 cbSave.setChecked(false); }else{ cbSave.setChecked(true); etName.setText(userName);//将获取到的值设置为text etPass.setText(userPass); } } /*点击事件保存数据*/ @Override public void onClick(View view) { switch (view.getId()) { case R.id.btLogin: String userName=etName.getText().toString().trim();//获取EditText中输入的值,并去掉空格 String userPass=etPass.getText().toString().trim(); if("admin".equals(userName)&&"123456".equals(userPass)){ if(cbSave.isChecked()){ editor.putString("userName",userName); editor.putString("userPass",userPass); editor.commit();//提交数据 }else{//若没有选择记住密码 editor.remove("userPass");//删除密码 editor.commit(); } Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: break; } } private void bindClick() { btLogin.setOnClickListener(this); btRegister.setOnClickListener(this); } }
5.程序运行就显示目标效果了。
您可能感兴趣的文章:
加载全部内容