在我们的程序开发过程中,经常会涉及到登录用户名密码获取数据等操作,在这些过程中,会验证某些条件是否满足,如果不满足就无法继续,但有时候我们需要看到各种初始状态或者一些界面效果,那么该怎么办呢?就是使用游客登录模式。
下面使用一个例子说明如何使用游客登录模式。
首先,做一个登录界面
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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //延迟一段时间后跳转到另一个界面 new Handler().postDelayed(new Runnable(){ @Override public 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; @Override protected 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() { @Override public 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.third); } }
界面效果
OK,简单介绍完了!