概括
安卓登录简单登录界面(用户名密码本地保存)时需要注意,校对的时候最好是先定义一个String类型的账号密码,在校对。不会出错。
核心代码
1 2
| String name_word="123456",password_word="123456"; !et_name.getText().toString().trim().equals(name_word)&&et_password.getText().toString().trim().equals(password_word)
|
详细代码
java文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.aclihui.six; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity { Button btlogin; Button btcancle; EditText et_name; EditText et_password; String name_word="123456",password_word="123456"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btlogin = (Button) findViewById(R.id.bt_login); btcancle = (Button) findViewById(R.id.bt_cancle); et_name = findViewById(R.id.et_name); et_password = findViewById(R.id.et_password); btcancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et_password.setText("123456"); et_name.setText("123456"); } }); btlogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!et_name.getText().toString().trim().equals(name_word)&&et_password.getText().toString().trim().equals(password_word)); { Toast.makeText(getApplicationContext(),"账号密码正确",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(MainActivity.this,front.class); startActivity(intent); } } });
} }
|
界面xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<EditText android:id="@+id/et_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="141dp" android:layout_marginTop="195dp" android:ems="10" android:inputType="textPersonName" android:text="请输入用户名" />
<Button android:id="@+id/bt_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/et_name" android:layout_alignParentEnd="true" android:layout_marginTop="143dp" android:layout_marginEnd="229dp" android:text="取消" />
<Button android:id="@+id/bt_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/bt_cancle" android:layout_alignParentTop="true" android:layout_marginStart="145dp" android:layout_marginTop="333dp" android:text="登录" />
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="130dp" android:layout_marginTop="31dp" tools:srcCompat="@tools:sample/avatars[2]" />
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="42dp" android:layout_marginTop="205dp" android:text="用户名" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginBottom="218dp" android:text="密码" />
<EditText android:id="@+id/et_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/et_name" android:layout_alignParentBottom="true" android:layout_marginBottom="200dp" android:ems="10" android:inputType="textPassword" android:text="请输入密码" /> </RelativeLayout>
|