1、登入界面设计
下图为登入界面的布局图,主要是EditText、Button等控件的布局设计。
2、页面布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="APP"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="18dp"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="100dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入账号"
android:maxLength="11"
android:minLines="1"
android:paddingLeft="5dp"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:id="@+id/zhanghao"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@android:color/darker_gray"/>
<EditText
android:id="@+id/mima"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLength="11"
android:maxLines="1"
android:paddingLeft="5dp"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@android:color/darker_gray"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginRight="23dp"
android:orientation="horizontal">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:text="显示密码"
android:id="@+id/checkbox"/>
<CheckBox
android:layout_marginLeft="140dp"
android:layout_width="120dp"
android:layout_height="40dp"
android:id="@+id/checkbox1"
android:text="记住密码"
android:checked="true"/>
</RelativeLayout>
<Button
android:id="@+id/dengru"
android:layout_width="match_parent"
android:layout_height="45dp"
style="@style/AlertDialog.AppCompat.Light"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="50dp"
android:text="登录"
android:onClick="querendengru"
android:textSize="18dp"
android:background="@android:color/holo_blue_light"
/>
</LinearLayout>
</RelativeLayout>
3、记住密码和显示密码功能
显示密码和记住密码都是通过CheckBox控件实现,其中显示密码是CheckBox中的setOnCheckedChangeListener实现,其代码如下:
checkBox = (CheckBox) findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//如果选中,显示密码
editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
//否则隐藏密码
editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
记住密码主要是使用content中的SharedPreferences方法实现数据的共享。
private SharedPreferences config;
config=getSharedPreferences("config",MODE_PRIVATE);
boolean isCheck=config.getBoolean("isCheck",false);
if (isCheck){
editText.setText(config.getString("user",""));
editText1.setText(config.getString("password",""));
}
String user=editText.getText().toString();
String password=editText1.getText().toString();
4、实现登入功能
登入功能实现主要是通过调用WebServer方法,实现用户信息的确认,如果账号和密码正确,调用WebServer方法返回True,然后可以通过Intent实现页面的跳转,否则为False,不跳转。这样可以方便手机与服务端的信息交流。
public void querendengru(View view)
{
String user=editText.getText().toString();
String password=editText1.getText().toString();
//Intent将数据传入下一个活动
/*Intent intent1=new Intent(activity_login.this,MainActivity.class);
intent1.putExtra("user",user);
startActivity(intent1);*/
if (user.length()!=0&&password.length()!=0)
{
SoapObject request = new SoapObject(NAMESPACE, METHON_NAME);
request.addProperty("loginusername", user);
request.addProperty("loginpassword", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.debug = true;
try
{
httpTransportSE.call(SOAP_ACTION, envelope);
}
catch (Exception e)
{
e.printStackTrace();
}
SoapObject object = (SoapObject) envelope.bodyIn;
String result = object.getProperty(0).toString();
if (result.equals("true"))
{
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "登入成功", Toast.LENGTH_LONG).show();
// finish();
SharedPreferences.Editor edit=config.edit();
boolean isCheck=checkBox1.isChecked();
edit.putBoolean("isCheck", isCheck);
if(isCheck){
edit.putString("user", user).putString("password", password);
}else{
edit.remove("username").remove("password");
}
//提交到本地
edit.commit();
}
else
{
Toast.makeText(getApplicationContext(), "服务连接失败,请查看网络或确认密码", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"账号或密码不能为空",Toast.LENGTH_LONG).show();
}
}