微信登录的完全源码,复制粘贴即可使用
作为Android学习的入门,一次性搞定Android的布局,让你的view层畅通无阻
手机界面
插入数据线,将手机调入开发者模式的usb调试,即可进行手机调试安装运行该APP
手机号登录![在这里插入图片描述](https://img-blog.csdnimg.cn/2021012209354644.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTMyMDIzOA==,size_16,color_FFFFFF,t_70)
代码实现:
MainActivity.java文件
public class MainActivity extends AppCompatActivity {
private Button nextButton;
private EditText editText;
private TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClick onClick=new OnClick();
nextButton=findViewById(R.id.next);
editText=findViewById(R.id.phone);
nextButton.setOnClickListener(onClick);
textView=findViewById(R.id.nextx);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// // 为Intent设置Action、Category属性
intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
startActivity(intent);
}
});
}
private class OnClick implements View.OnClickListener {
@Override
public void onClick(View v) {
if(editText.getText().length()==11){
Intent i = new Intent();
i.setClass(MainActivity.this , LoginActivity.class);
i.putExtra("phone",editText.getText().toString());
startActivity(i);
}else {
Toast.makeText(MainActivity.this, "提示的内容", Toast.LENGTH_LONG).show();
}
}
}
}
activity_main.xml文件内容
<?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"
android:orientation="vertical"
tools:context="com.example.wechatlogin.MainActivity">
<TextView
android:id="@+id/nextx"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:layout_marginBottom="20dp"
android:text="×"
android:textSize="29sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:text="手机号登录"
android:textSize="29sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingTop="20dp"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:paddingLeft="10dp"
android:text="国家/地区"
android:textSize="18sp" />
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:paddingLeft="10dp"
android:text="中国大陆(+86)"
android:textColor="#11E811"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:layout_marginRight="10dp"
android:paddingTop="15dp"
android:text="手机号"
android:textSize="18sp" />
<EditText
android:id="@+id/phone"
android:layout_width="310dp"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:hint="请输入目的地" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
android:text="用微信号/QQ号/邮箱登录"
android:textSize="14sp" />
<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0FC814"
android:text="下一步"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="400dp"
android:paddingTop="10dp"
android:text="找回密码 | 紧急冻结 | 微信安全中心" />
</LinearLayout>
验证码登录(包含倒计时、framement切换)
loginActivity.java文件
public class LoginActivity extends AppCompatActivity {
private OnClick onClick;
private EditText phoneEdit;
private EditText passEdit;
private EditText verifyEdit;
private Button verifyButton;
private Button loginButton;
private TextView timeView;
private LinearLayout passLinear;
private LinearLayout verifyLinear;
private RelativeLayout replaceVerify;
private RelativeLayout replacePass;
private String phone;
private CountDownTimer verifyTime;
private TextView passView;
private TextView textView;
private TextView textFView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
onClick=new OnClick();
phoneEdit=findViewById(R.id.loginphone);
verifyEdit=findViewById(R.id.verifylogin);
verifyButton=findViewById(R.id.verifybtn);
loginButton=findViewById(R.id.loginbut);
timeView=findViewById(R.id.timeview);
replacePass=findViewById(R.id.replacepass);
replaceVerify=findViewById(R.id.replaceverify);
textView=findViewById(R.id.textview);
passLinear=findViewById(R.id.passlinear);
verifyLinear=findViewById(R.id.verilinear);
phone=getIntent().getStringExtra("phone");
phoneEdit.setText(phone);
phoneEdit.setEnabled(false);
textFView=findViewById(R.id.loginxx);
textFView.setOnClickListener(new View.OnClickListener() {//X的退出
@Override
public void onClick(View v) {
Intent intent = new Intent();
// // 为Intent设置Action、Category属性
intent.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
intent.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
startActivity(intent);
}
});
verifyButton.setOnClickListener(onClick);
loginButton.setOnClickListener(new View.OnClickListener(){//登录按钮
@Override
public void onClick(View v) {
if(verifyEdit.getText().toString().equals("1234")){
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
Intent intent=new Intent();
intent.setClass(LoginActivity.this,WeChatActivity.class);
startActivity(intent);
}else {
new AlertDialog.Builder(LoginActivity.this).setTitle("验证码错误!").setMessage("请输入正确的验证码").setPositiveButton("确定",null).show();
}
}
});
textView.setOnClickListener(new View.OnClickListener() {//切换密码按钮
@Override
public void onClick(View v) {
Toast.makeText(LoginActivity.this, "试一次吧", Toast.LENGTH_LONG).show();
if(textView.getText().equals("用密码登录")){
passLinear.setVisibility(View.INVISIBLE);
verifyLinear.setVisibility(View.VISIBLE);
textView.setText("用手机号登录");
}else {
passLinear.setVisibility(View.VISIBLE);
verifyLinear.setVisibility(View.INVISIBLE);
textView.setText("用密码登录");
}
}
});
}
private class OnClick implements View.OnClickListener {//验证按钮事件
@Override
public void onClick(View v) {//退回界面
verifyButton.setVisibility(View.INVISIBLE);
timeView.setVisibility(View.VISIBLE);
// verifyTime=new VerifyTime(verifyButton,timeView,10000,1000);
verifyTime = new CountDownTimer(10 * 1000, 1000) {//验证码倒计时
@Override
public void onTick(long millisUntilFinished) {
timeView.setClickable(false);
timeView.setText(millisUntilFinished / 1000 + "s后重新获取" );
timeView.setBackgroundColor(Color.GRAY);
}
@Override
public void onFinish() {
verifyButton.setVisibility(View.VISIBLE);
timeView.setVisibility(View.INVISIBLE);
}
};
verifyTime.start();
}
}
}
activity_login.xml文件
<?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"
android:orientation="vertical"
tools:context="com.example.wechatlogin.LoginActivity">
<TextView
android:id="@+id/loginxx"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:layout_marginBottom="20dp"
android:text="×"
android:textSize="29sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:text="手机号登录"
android:textSize="29sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_marginRight="8dp"
android:contentDescription="目的地"
android:labelFor="@+id/resource"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="手机号"
android:textSize="18sp" />
<EditText
android:id="@+id/loginphone"
android:layout_width="310dp"
android:layout_height="match_parent"
android:hint="+8613168090721"
android:paddingTop="8dp" />
</LinearLayout>
<RelativeLayout
android:id="@+id/replacepass"
android:layout_width="match_parent"
android:layout_height="58dp">
<LinearLayout
android:id="@+id/passlinear"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_marginRight="8dp"
android:labelFor="@+id/resource"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="密码"
android:textSize="18sp" />
<EditText
android:id="@+id/loginpass"
android:layout_width="310dp"
android:layout_height="match_parent"
android:hint="请输入密码"
android:paddingTop="8dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/verilinear"
android:layout_width="match_parent"
android:layout_height="58dp"
android:visibility="invisible"
android:layout_marginRight="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="验证码"
android:textSize="18sp" />
<EditText
android:id="@+id/verifylogin"
android:layout_width="150dp"
android:layout_height="match_parent"
android:hint="请输入验证码"
android:paddingTop="8dp" />
<RelativeLayout
android:id="@+id/replaceverify"
android:layout_width="150dp"
android:layout_height="match_parent">
<Button
android:id="@+id/verifybtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#0FC814"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="获取验证码"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/timeview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
android:text="用密码登录"
android:textColor="#FF0FC814"
android:textSize="14sp" />
<Button
android:id="@+id/loginbut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="登录"
android:background="#0FC814"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="400dp"
android:gravity="center_horizontal"
android:text="找回密码 | 紧急冻结 | 微信安全中心" />
</LinearLayout>
倒计时工具类
VerifyTimer.java
package com.example.wechatlogin;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class VerifyTime extends CountDownTimer {
private TextView textView;
private Button button;
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public VerifyTime(Button button,TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textView=textView;
this.button=button;
}
@Override
public void onTick(long millisUntilFinished) {
textView.setClickable(false);
textView.setText(millisUntilFinished / 1000 + "s后重新获取" );
textView.setBackgroundColor(Color.GRAY);
}
@Override
public void onFinish() {
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.INVISIBLE);
}
}
登录之后的跳转界面(我是随便写的一个空界面)
WeChatActivity.java
public class WeChatActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_we_chat);
}
}
activity_we_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="com.example.wechatlogin.WeChatActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
引用的drawable文件的图片就靠你自己随便添加喽!
这样一个微信登录界面就做好了,有问题随时留言欧!