- 创建一个androidstudio工程
- 在
drawable
中添加
main_left
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/orange">
</stroke>
<corners
android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp">
</corners>
</shape>
main_right
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/orange">
</stroke>
<corners
android:bottomRightRadius="20dp"
android:topRightRadius="20dp">
</corners>
</shape>
- 在
activity_main
中完成基本布局代码
``
<TextView
android:id="@+id/tv_mian_1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="登录界面"
android:gravity="center"
android:textSize="30sp"
android:background="#ff8f03"
android:layout_margin="20dp"/>
<EditText
android:id="@+id/et_username1"
android:layout_width="300dp"
android:layout_height="50dp"
android:hint="用户名"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_margin="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_password1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="密 码"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_marginLeft="30dp"
android:inputType="textPassword"/>
<ImageView
android:layout_width="20dp"
android:id="@+id/eye"
android:layout_height="20dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btn_main_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录"
android:background="@drawable/main_left"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/btn_main_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册"
android:background="@drawable/main_right"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
``
- 完成登录后界面和注册时界面
activity_login
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".LoginActivity"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_login_1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="登录后界面"
android:gravity="center"
android:textSize="30sp"
android:background="#feeeee"
android:layout_margin="20dp"/>
</LinearLayout>
activity_register
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".RegisterActivity"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_register_1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="注册界面"
android:gravity="center"
android:textSize="30sp"
android:background="#ff8ff3"
android:layout_margin="20dp"/>
<EditText
android:id="@+id/et_register_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="用户名"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_margin="20dp" />
<EditText
android:id="@+id/et_register_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="设置密码"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:inputType="textPassword"/>
<EditText
android:id="@+id/et_register_3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="确认密码"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:inputType="textPassword"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_register_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:layout_marginTop="20dp"
/>
</LinearLayout>
- 其次完成MainActivity和RegisterActivity的代码
- 先在网上下载两个小眼睛完成输入密码后点击后查看密码插入到
res-mipmap
MainActivity
```
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//声明控件
private Button mBtn_main_1;
private Button mBtn_main_2;
private EditText mEt_username1;
private EditText mEt_password1;
private Object vi;
private boolean hide = true;
private ImageView eye;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
mBtn_main_1 = findViewById(R.id.btn_main_1);
mBtn_main_2 = findViewById(R.id.btn_main_2);
mEt_username1 = findViewById(R.id.et_username1);
mEt_password1 = findViewById(R.id.et_password1);
eye = findViewById(R.id.eye);
eye.setImageResource(R.mipmap.close);
eye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.eye:
if (hide == true) {
eye.setImageResource(R.mipmap.open);
HideReturnsTransformationMethod method = HideReturnsTransformationMethod.getInstance();
mEt_password1.setTransformationMethod(method);
boolean b = hide == false;
} else {
eye.setImageResource(R.mipmap.close);
TransformationMethod method = PasswordTransformationMethod.getInstance();
mEt_password1.setTransformationMethod(method);
boolean b = hide == true;
}
int index = mEt_password1.getText().toString().length();
mEt_password1.setSelection(index);
break;
}
}
});
/*mBtn_main_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});*/
mBtn_main_1.setOnClickListener(this);
mBtn_main_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
}
public void onClick(View view) {
//从RegisterActivity中带过来的用户名和密码
Intent intent = getIntent();
String name = intent.getStringExtra("username");
String pass = intent.getStringExtra("password");
//从本界面activity_main中获取输入的用户名和密码
String username1 = mEt_username1.getText().toString();
String password1 = mEt_password1.getText().toString();
Intent intent1 = null;
if (username1.equals(name) && password1.equals(pass)){
intent1 = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent1);
}else {
}
}
}
```
RegisterACtivity
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class RegisterActivity extends AppCompatActivity {
private Button mBtn_register_1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mBtn_register_1 = findViewById(R.id.btn_register_1);
mBtn_register_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
//利用方法生成用户名和密码
intent.putExtra("username","abc");
intent.putExtra("password","12");
startActivity(intent);
}
});
}
}
标签:界面,登录,Intent,import,android,main,intent,View
From: https://www.cnblogs.com/LLever/p/16814893.html