Detailed explanation of cloud platform login code
Open app
The effect is shown in the figure:
First, build some custom views for the Switch control, and create six files in the drawable folder, respectively: open_track, open_thumb, close_track, close_thumb; track, thumb
open_track code is as follows:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 高度30 此处设置宽度无效-->
<size android:height="40dp"/>
<!-- 圆角弧度 15 -->
<corners android:radius="15dp"/>
<!-- 变化率 定义从左到右的颜色不变 -->
<gradient
android:endColor="#66ff33"
android:startColor="#66ff33" />
</shape>
The close_track code is as follows:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="40dp" android:width="40dp"/>
<corners android:radius="20dp"/>
<gradient android:startColor="#eeeeee"
android:endColor="#eeeeee"/>
<stroke android:width="1dp"
android:color="#666666"/>
</shape>
The open_thumb code is as follows:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="40dp" android:width="40dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#33da33"/>
</shape>
The close_thumb code is as follows:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle" >
<size android:height="40dp" android:width="40dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="20dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#666666"/>
</shape>
The track code is as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/open_track"/>
<item android:drawable="@drawable/shut_track"/>
</selector>
The thumb code is as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/open_thumb"/>
<item android:drawable="@drawable/shut_thumb"/>
</selector>
Finally applied to the layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background1">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/home"
android:layout_gravity="center"
android:layout_marginTop="100dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="智能家居"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_marginLeft="100dp"
android:textColor="#00ffcc"
android:layout_marginTop="20dp"
/>
<Switch
android:id="@+id/Open"
android:layout_width="100dp"
android:layout_height="100dp"
android:thumb="@drawable/thumb"
android:track="@drawable/track"
android:layout_gravity="center"
android:layout_marginTop="50dp"/>
</LinearLayout>
Log in to the cloud platform
Create a Login activity The
effect diagram is as shown in the figure below:
Click the little eye next to it to view the password
login.xml code as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:src="@drawable/home"
android:scaleType="fitStart"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="智能家居"
android:gravity="center"
android:layout_gravity="center"
android:textSize="30sp"
android:textColor="#000000"
android:layout_marginBottom="30dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="账 号:"
android:textSize="25sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/username"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/passwordbox"
android:hint="用户名"
android:paddingLeft="20dp"
android:textColor="#ff000000"
android:textSize="20sp"
android:layout_marginLeft="0dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
>
<TextView
android:id="@+id/TipsPassWord"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="密 码:"
android:textSize="25sp"
android:textColor="#000000"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/password"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/passwordbox"
android:hint="密 码"
android:paddingLeft="22dp"
android:textColor="#ff000000"
android:textSize="20sp"
android:password="true"
android:layout_toRightOf="@+id/TipsPassWord"
android:layout_marginTop="10dp"/>
<ImageView
android:id="@+id/notseethepassword"
android:layout_width="60dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:background="@drawable/passwordbox"
android:layout_toRightOf="@+id/password"
android:layout_marginTop="10dp"/>
<!--<ImageButton
android:id="@+id/seethepassword"
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/openeye"
android:scaleType="fitCenter"
android:background="#ffffff"
android:layout_marginLeft="320dp"
android:visibility="invisible"
/>-->
</RelativeLayout>
<Button
android:id="@+id/login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="登 陆"
android:textColor="#ffffffff"
android:textSize="20sp"
android:background="@drawable/login"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Let’s take a look at the code for how to make eyes smaller.
Set a boolean variable to determine whether to display the password.
private boolean Smalleye = true;
private void OnClick(){
OnClick onClick = new OnClick();
Login.setOnClickListener(onClick);
//SeeThePassWord.setOnClickListener(onClick);
NotSeePassWord.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
SignIn();
break;
case R.id.notseethepassword:
if (Smalleye == true)
{
SeeThePassWordMethod(); //显示密码
Smalleye = !Smalleye;
}else {
NotSeeThePassWordMethod(); //隐藏密码
Smalleye = !Smalleye;
}
break;
}
}
}
private void NotSeeThePassWordMethod(){
NotSeePassWord.setImageResource(R.drawable.closeeye);
PassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
private void SeeThePassWordMethod(){
NotSeePassWord.setImageResource(R.drawable.openeye);
PassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
The following is the code for logging in to the cloud platform:
private void SignIn(){
String platformAddress = NewloadParameter.IP_DEFAULT_VALUE; //网址加端口号
String LoginUserName = UserName.getText().toString();
String LoginPassWord = PassWord.getText().toString();
if (TextUtils.isEmpty(platformAddress)){
Toast.makeText(this,"请配置新大陆云平台信息",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(LoginUserName) || TextUtils.isEmpty(LoginPassWord)){
Toast.makeText(this,"账号或者密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
NetWorkBusiness netWorkBusiness = new NetWorkBusiness("",platformAddress);
netWorkBusiness.signIn(new SignIn(LoginUserName, LoginPassWord), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity<User> response) {
}
@Override
public void onResponse(Call<BaseResponseEntity<User>> call, Response<BaseResponseEntity<User>> response) {
super.onResponse(call, response);
BaseResponseEntity<User> baseResponseEntity = response.body(); //获取请求
if (baseResponseEntity != null){
//获取访问令牌
String accestoken = baseResponseEntity.getResultObj().getAccessToken();
Intent intent = new Intent(Login.this,MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("accestoken",accestoken);
intent.putExtras(bundle); // 传递令牌
startActivity(intent);
finish();
}
}
});
}
The following is the entire code:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class Login extends AppCompatActivity {
private Button Login;
private EditText UserName,PassWord;
private ImageView NotSeePassWord;
private boolean Smalleye = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
InitView();
OnClick();
NotSeePassWord.setImageResource(R.drawable.closeeye);
}
private void InitView(){
Login = findViewById(R.id.login);
UserName = findViewById(R.id.username);
PassWord = findViewById(R.id.password);
// SeeThePassWord = findViewById(R.id.seethepassword);
NotSeePassWord = findViewById(R.id.notseethepassword);
}
private void OnClick(){
OnClick onClick = new OnClick();
Login.setOnClickListener(onClick);
//SeeThePassWord.setOnClickListener(onClick);
NotSeePassWord.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
SignIn();
break;
case R.id.notseethepassword:
if (Smalleye == true)
{
SeeThePassWordMethod();
Smalleye = !Smalleye;
}else {
NotSeeThePassWordMethod();
Smalleye = !Smalleye;
}
break;
}
}
}
private void NotSeeThePassWordMethod(){
NotSeePassWord.setImageResource(R.drawable.closeeye);
PassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
private void SeeThePassWordMethod(){
NotSeePassWord.setImageResource(R.drawable.openeye);
PassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
private void SignIn(){
String platformAddress = NewloadParameter.IP_DEFAULT_VALUE; //网址加端口号
String LoginUserName = UserName.getText().toString();
String LoginPassWord = PassWord.getText().toString();
if (TextUtils.isEmpty(platformAddress)){
Toast.makeText(this,"请配置新大陆云平台信息",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(LoginUserName) || TextUtils.isEmpty(LoginPassWord)){
Toast.makeText(this,"账号或者密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
NetWorkBusiness netWorkBusiness = new NetWorkBusiness("",platformAddress);
netWorkBusiness.signIn(new SignIn(LoginUserName, LoginPassWord), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity<User> response) {
}
@Override
public void onResponse(Call<BaseResponseEntity<User>> call, Response<BaseResponseEntity<User>> response) {
super.onResponse(call, response);
BaseResponseEntity<User> baseResponseEntity = response.body(); //获取请求
if (baseResponseEntity != null){
//获取访问令牌
String accestoken = baseResponseEntity.getResultObj().getAccessToken();
Intent intent = new Intent(Login.this,MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("accestoken",accestoken);
intent.putExtras(bundle); // 传递令牌
startActivity(intent);
finish();
}
}
});
}
}
At first, I used AVD to run the program, but it couldn't run, and then I ran it with the Thunderbolt Simulator and the login was successful.
Then you can see that the application you created is successfully online in the Cloud Platform Developer Center