安卓开发
概述:该代码实现了账户的输入和判断,界面的跳转,和密码的显示与隐藏.
1:效果展示
2:重要代码展示
设置布局
设置输入框
<EditText 账号输入框android:id="@+id/edit1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:layout_marginTop="252dp"android:layout_marginRight="30dp"android:hint="请输入账户"android:inputType="text" />
<EditText 密码输入框android:id="@+id/edit2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:layout_marginTop="350dp"android:layout_marginRight="30dp"android:hint="请输入密码"android:inputType="numberPassword" />
<ImageView 点击显示密码出现对钩android:id="@+id/annui"android:layout_width="30dp"android:layout_height="33dp"android:layout_marginLeft="50dp"android:layout_marginTop="420dp"android:background="@mipmap/duihao"android:visibility="invisible" />
设置按钮
<ToggleButton 显示密码按钮android:id="@+id/button1"android:layout_width="30dp"android:layout_height="33dp"android:layout_marginLeft="50dp"android:layout_marginTop="420dp"android:background="@mipmap/zhengfangxing"android:text="@null"android:textOff=""android:textOn="" />
<Button 登录按钮android:id="@+id/button3"android:layout_width="100dp"android:layout_height="70dp"android:text="点击登录"android:layout_marginTop="10dp"android:layout_gravity="center_horizontal"android:onClick="dianji"></Button>
<Button 第二界面的跳转按钮 android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="55dp"android:text="重新登陆"android:background="@mipmap/muban"android:onClick="dianji2"/>
3:功能实现
实现点击显示密码按钮出现对钩并显示密码 ToggleButton toggleButton = findViewById(R.id.button1); EditText mima = findViewById(R.id.edit2); ImageView an1 = findViewById(R.id.annui); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {if(b){//显示密码mima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());an1.setVisibility(View.VISIBLE);}else {//隐藏密码mima.setTransformationMethod(PasswordTransformationMethod.getInstance());an1.setVisibility(View.INVISIBLE);} } });
实现点击监听事件,判断登录密码与账号是否正确,并跳转到第二界面
public void dianji(View v){// 获取账户/密码EditText nameEdi = findViewById(R.id.edit1);EditText pasEdi = findViewById(R.id.edit2);String name = nameEdi.getText().toString();String pass = pasEdi.getText().toString();if(name.equals("admin") && pass.equals("123456")){Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();//新建一个Intent(当前Activity, SecondActivity)=====显示IntentIntent intent = new Intent(MainActivity.this ,sce_MainActivity2.class);//启动startActivity(intent);}else {Toast.makeText(this,"账户或密码错误",Toast.LENGTH_SHORT).show();}}
从第二界面返回第一界面
package com.example.protect001;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.View;public class sce_MainActivity2 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.scedent_layout);} //界面跳转public void dianji2(View v){finish();} }
安卓开发
概述:该代码实现了账户的输入和判断,界面的跳转,和密码的显示与隐藏.
1:效果展示
2:重要代码展示
设置布局
设置输入框
<EditText 账号输入框android:id="@+id/edit1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:layout_marginTop="252dp"android:layout_marginRight="30dp"android:hint="请输入账户"android:inputType="text" />
<EditText 密码输入框android:id="@+id/edit2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="100dp"android:layout_marginTop="350dp"android:layout_marginRight="30dp"android:hint="请输入密码"android:inputType="numberPassword" />
<ImageView 点击显示密码出现对钩android:id="@+id/annui"android:layout_width="30dp"android:layout_height="33dp"android:layout_marginLeft="50dp"android:layout_marginTop="420dp"android:background="@mipmap/duihao"android:visibility="invisible" />
设置按钮
<ToggleButton 显示密码按钮android:id="@+id/button1"android:layout_width="30dp"android:layout_height="33dp"android:layout_marginLeft="50dp"android:layout_marginTop="420dp"android:background="@mipmap/zhengfangxing"android:text="@null"android:textOff=""android:textOn="" />
<Button 登录按钮android:id="@+id/button3"android:layout_width="100dp"android:layout_height="70dp"android:text="点击登录"android:layout_marginTop="10dp"android:layout_gravity="center_horizontal"android:onClick="dianji"></Button>
<Button 第二界面的跳转按钮 android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="55dp"android:text="重新登陆"android:background="@mipmap/muban"android:onClick="dianji2"/>
3:功能实现
实现点击显示密码按钮出现对钩并显示密码 ToggleButton toggleButton = findViewById(R.id.button1); EditText mima = findViewById(R.id.edit2); ImageView an1 = findViewById(R.id.annui); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {if(b){//显示密码mima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());an1.setVisibility(View.VISIBLE);}else {//隐藏密码mima.setTransformationMethod(PasswordTransformationMethod.getInstance());an1.setVisibility(View.INVISIBLE);} } });
实现点击监听事件,判断登录密码与账号是否正确,并跳转到第二界面
public void dianji(View v){// 获取账户/密码EditText nameEdi = findViewById(R.id.edit1);EditText pasEdi = findViewById(R.id.edit2);String name = nameEdi.getText().toString();String pass = pasEdi.getText().toString();if(name.equals("admin") && pass.equals("123456")){Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();//新建一个Intent(当前Activity, SecondActivity)=====显示IntentIntent intent = new Intent(MainActivity.this ,sce_MainActivity2.class);//启动startActivity(intent);}else {Toast.makeText(this,"账户或密码错误",Toast.LENGTH_SHORT).show();}}
从第二界面返回第一界面
package com.example.protect001;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.View;public class sce_MainActivity2 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.scedent_layout);} //界面跳转public void dianji2(View v){finish();} }