Android之游客登录模式
来源:互联网 发布:手机淘宝客户端下载 编辑:程序博客网 时间:2024/05/01 03:44
在我们的程序开发过程中,经常会涉及到登录用户名密码获取数据等操作,在这些过程中,会验证某些条件是否满足,如果不满足就无法继续,但有时候我们需要看到各种初始状态或者一些界面效果,那么该怎么办呢?就是使用游客登录模式。
下面使用一个例子说明如何使用游客登录模式。
首先,做一个登录界面
activity_main.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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" android:background="#ff0" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="欢迎界面" android:textColor="#00f" android:textSize="50sp"/></LinearLayout>MainActivity.java
package com.guestlogindemo;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//延迟一段时间后跳转到另一个界面new Handler().postDelayed(new Runnable(){@Overridepublic void run(){Intent intent = new Intent (MainActivity.this,SecondActivity.class);startActivity(intent);//跳转界面MainActivity.this.finish();//关闭此界面}}, 1000);}}界面效果
然后,第二个界面
second.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingBottom="6dp"android:paddingLeft="6dp"android:paddingRight="6dp"android:paddingTop="6dp" android:gravity="center_horizontal" android:background="#000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#fff" android:text="登录"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="15sp"/> <EditText android:id="@+id/user_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="用户名"/> <View android:layout_width="fill_parent" android:layout_height="15sp"/> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="密码"/> <View android:layout_width="fill_parent" android:layout_height="15sp"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="游客登录" android:onClick="guest_btn"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"android:text="登录"android:onClick="login_btn"/> </LinearLayout> </LinearLayout></LinearLayout>SecondActivity.java
package com.guestlogindemo;import android.annotation.SuppressLint;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.os.SystemClock;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class SecondActivity extends Activity{private EditText mUserName = null;private EditText mPassword = null;public static boolean bIsGuestLogin = false;//是否游客登录,这个变量用于其他界面数据的处理ProgressDialog m_Dialog;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second);mUserName = (EditText)findViewById(R.id.user_name);mPassword = (EditText)findViewById(R.id.password);}public void guest_btn(View view){bIsGuestLogin = true;Intent intent = new Intent(); intent.setClass(SecondActivity.this,ThirdActivity.class); startActivity(intent); Toast.makeText(this, "您现在是以游客身份登录,登录成功", Toast.LENGTH_SHORT).show(); SecondActivity.this.finish();}@SuppressLint("NewApi")public void login_btn(View view){bIsGuestLogin = false;if (("sl".equals(mUserName.getText().toString()) && "123456".equals(mPassword.getText().toString())))//判断输入的值是否正确{m_Dialog = ProgressDialog.show(this, "请等待...", "正在登录...", true);new Thread(){public void run(){try{SystemClock.sleep(2000);Intent intent = new Intent(); intent.setClass(SecondActivity.this,ThirdActivity.class); startActivity(intent); SecondActivity.this.finish();} catch (Exception e){e.printStackTrace();}finally{m_Dialog.dismiss();}}}.start();}else if (mUserName.getText().toString().isEmpty() || mPassword.getText().toString().isEmpty())//判断是否输入相关值{AlertDialog dialog = new AlertDialog.Builder(this).setTitle("登录").setMessage("请输入相关值").setPositiveButton("确定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){dialog.dismiss();}}).create();dialog.show();}else{new AlertDialog.Builder(SecondActivity.this).setTitle("登录失败").setMessage("帐号或者密码不正确,\n请检查后重新输入!").create().show();}}}界面效果
第二个界面有两种登录模式,一种是登录模式,一种是游客登录模式
登录模式需要验证各种信息,而游客登录模式则不需要验证信息,可以直接登录,但是在这种情况下登录的只能查看界面效果,不能处理任何数据
游客登录模式直接进入第三个界面
而登录模式则需要验证各种信息才能进入,如下空值情况
错误值情况
正确值情况
最后,最后一个界面,用于第二个界面跳转
third.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#000" ></LinearLayout>ThirdActivity.java
package com.guestlogindemo;import android.app.Activity;import android.os.Bundle;public class ThirdActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.third);}}界面效果
OK,简单介绍完了!
源码下载
0 1
- Android之游客登录模式
- iOS中游客模式的自动登录“正确姿势”
- discuz论坛不登录状态(游客浏览)模式下,门户页面显示keyword关键词,description描述
- Discuz!教程之设置游客看小图、登录/注册后可看大图的方法
- android activity登录模式
- Android之MVP模式实现登录和网络数据加载
- 游客好久了,切换到博客模式
- session统计在线人数,查看游客列表,登录人列表
- 13 ubuntu丢失root权限 只能用游客登录解决方案
- Ubuntu16.04 只能以游客身份登录问题
- Android之QQ登录
- Android 之登录界面
- Android之登录界面
- 只能游客登录以及ubuntu输入密码登录后继续回到登录界面
- 游客模式,记住当前点击的位置,登陆成功后回调。
- csdn待改进点之31------>无登录态时去点赞,点不了是合理的(基于游客不能点赞), 但为什么不给用户提示? 为什么不直接跳转到登录页?
- ubuntu12.04 正常用户输入密码无法登录游客可以正常登录的解决方法
- Android之SqliteDatabase(MVP模式)实现用户登录注册功能
- JFrame中的setDefaultCloseOperation
- 第1章 对象导论
- unity学习之数据库基础
- android工程的mk写法。
- 从Samples中入门IOS开发(四)------ 基于socket的网络编程
- Android之游客登录模式
- 第15周项目3-在OJ上玩指针-(4)
- 杭电2209 翻纸牌游戏(BFS)
- 除法计算-C++-非*,/,%运算
- 让HorizontalScrollView里某个控件占满整个屏幕
- 系统自动导入android.R包
- ASIHTTPRequest发送异步GET请求
- 形状特征——Sift特征
- LinuxMint笔记:eclipse安装使用