安卓登录注册简单实现

发布时间:2023-12-24 08:30

目录

 login.xml

Login.class

网络请求工具类

arrays.xml

Register.class

网络请求工具类

个人信息查看

java代码

信息修改工具类

密码修改布局

java代码

工具类

主页面展示

主页面代码

信息校验工具类


 login.xml




    
        
    
    

        

        

            

                

            
        

        


        
            
        

        

    
    

        

Login.class

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.smartcity_c.MainActivity;
import com.example.smartcity_c.R;
import com.example.smartcity_c.utils.Youzi;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity {

    private EditText et_acconut, et_password;
    private LinearLayout lay1, lay2;
    private ImageView imageView;
    private Button button;
    private Youzi youzi = new Youzi();
    private Map map = new HashMap();
    private String account,password;
    private String token;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private TextView gologin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_acconut = findViewById(R.id.login_a_et_account);
        et_password = findViewById(R.id.login_a_et_password);
        lay1 = findViewById(R.id.login_a_lay_1);
        lay2 = findViewById(R.id.login_a_lay_2);

        button = findViewById(R.id.login_a_submit);
        gologin = findViewById(R.id.login_goregister);

        sharedPreferences = getSharedPreferences(\"user\",MODE_PRIVATE);
        editor = sharedPreferences.edit();

        Intent intent = getIntent();
        String username = intent.getStringExtra(\"username\");
        String passwordd = intent.getStringExtra(\"password\");
        et_acconut.setText(username);
        et_password.setText(passwordd);

        imageView = findViewById(R.id.login_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        et_acconut.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay1.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay2.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        et_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay2.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay1.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        gologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this,Retister.class));
            }
        });

        login();

    }
    private void login(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = et_acconut.getText().toString();
                password = et_password.getText().toString();
                if (!account.isEmpty()&&account!=null&&account!=\"\"&&!password.isEmpty()&&password!=null&&password!=\"\"){
                    map.put(\"username\", account);
                    map.put(\"password\", password);
                    final String mapJson = JSON.toJSONString(map);


                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                               final JSONObject jsonObject = youzi.postsend(mapJson);
                               final String s = jsonObject.getString(\"msg\");

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (s.equals(\"操作成功\")){
                                            try {
                                                token = jsonObject.getString(\"token\");
                                                editor.putBoolean(\"check\",true);
                                                editor.putString(\"token\",token);
                                                editor.putString(\"username\",account);
                                                editor.commit();
                                                Toast.makeText(Login.this, \"登录成功\", Toast.LENGTH_SHORT).show();
                                                Intent intent = new Intent(Login.this, MainActivity.class);
                                                intent.putExtra(\"username\",account);
                                                startActivity(intent);
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }

                                        }else {

                                            Toast.makeText(Login.this, s, Toast.LENGTH_SHORT).show();
                                        }
                                        System.out.println(mapJson);
                                    }
                                });

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();

                }else {
                    Toast.makeText(Login.this, \"请输入完整信息\", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
}

网络请求工具类

 public JSONObject postsend(String json) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse(\"application/json\");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method(\"POST\", body)
                .addHeader(\"Content-Type\", \"application/json\")
                .build();
        Response response = client.newCall(request).execute();
        String s = response.body().string();
        JSONObject jsonObject = new JSONObject(s);
        return jsonObject;
    }

register.xml




    
        
    
    

        

        

            

                

            
        

        

        

            

                

            
        

        


        
            
        

        

        

            

                

            
        

        
        

            

                
                

            
        

        


    
    

        

arrays.xml




    
        
        
    

Register.class

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.smartcity_c.R;
import com.example.smartcity_c.utils.IsCarnumberNO;
import com.example.smartcity_c.utils.Youzi;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {

    private ImageView back;
    private EditText editText1,editText2,editText3,editText4;
    private LinearLayout lay1,lay2,lay3,lay5;
    private Spinner spinner;
    private String sex,username,password,phone,nickname;
    private Button submit;
    private Youzi youzi = new Youzi();
    private Map map = new HashMap();
    private int sexx=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retister);

        back = findViewById(R.id.register_back);
        editText1 = findViewById(R.id.register_et_account);
        editText2 = findViewById(R.id.register_et_password);
        editText3 = findViewById(R.id.register_et_phone);
        editText4 = findViewById(R.id.register_et_nickname);
        lay5 =findViewById(R.id.register_lay_5);

        spinner = findViewById(R.id.register_sex);
        submit = findViewById(R.id.register_submit);

        
        lay1 = findViewById(R.id.register_lay_1);
        lay2 = findViewById(R.id.register_lay_2);
        lay3 = findViewById(R.id.register_lay_3);

        editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay1.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay2.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay3.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay5.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay2.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay1.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay3.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay5.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        editText3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay3.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay2.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay1.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay5.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });
        editText4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay5.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay2.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay1.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                    lay3.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        register();
    }
    
    private void register(){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = editText1.getText().toString();
                password = editText2.getText().toString();
                phone = editText3.getText().toString();
                sex = spinner.getSelectedItem().toString();
                nickname = editText4.getText().toString();
                if (!username.isEmpty()&&username!=null&&username!=\"\"&&!password.isEmpty()&&
                        password!=null&&password!=\"\"&&!phone.isEmpty()&&phone!=null&&phone!=\"\"&&!nickname.isEmpty()&&nickname!=null&&nickname!=\"\"){
                    if (sex.equals(\"男\")){
                        sexx = 1;
                    }else {
                        sexx = 0;
                    }

                    boolean falg = new IsCarnumberNO().isphone(phone);

                    if (falg){
                        map.put(\"userName\",username);
                        map.put(\"password\",password);
                        map.put(\"phonenumber\",phone);
                        map.put(\"sex\",sexx);
                        map.put(\"nickName\",nickname);
                        final String mapJson = JSON.toJSONString(map);

                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                                try {
                                    JSONObject jsonObject = youzi.register(mapJson);
                                    final String msg = jsonObject.getString(\"msg\");

                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                            if (msg.equals(\"操作成功\")){
                                                Intent intent = new Intent(Register.this,Login.class);
                                                intent.putExtra(\"username\",username);
                                                intent.putExtra(\"password\",password);
                                                startActivity(intent);
                                                Toast.makeText(Register.this, \"注册成功\", Toast.LENGTH_SHORT).show();

                                            }else {
                                                Toast.makeText(Register.this, msg, Toast.LENGTH_SHORT).show();
                                            }
                                            System.out.println(mapJson);
                                        }
                                    });

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }).start();
                    }else {
                        Toast.makeText(Register.this, \"请输入正确格式手机号\", Toast.LENGTH_SHORT).show();
                    }




                }else {
                    Toast.makeText(Register.this, \"请填写完整信息!\", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

网络请求工具类

 public JSONObject register(String json) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse(\"application/json\");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method(\"POST\", body)
                .addHeader(\"Content-Type\", \"application/json\")
                .build();
        Response response = client.newCall(request).execute();
        String s = response.body().string();
        JSONObject jsonObject = new JSONObject(s);
        return jsonObject;
    }

个人信息查看



        
            

        


        

            
        

    
        
        
        
            
        
    

        
            
            
                
            
        

        
            
            
                
            
        

        
            
            
                
            
        

        

            

                
                
                    
                


            
        




    


        

            

java代码

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.smartcity_c.R;
import com.example.smartcity_c.utils.IsCarnumberNO;
import com.example.smartcity_c.utils.Youzi;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class GeRenXinxi extends AppCompatActivity {

    private ImageView back;
    private SharedPreferences sharedPreferences;
    private String token;
    private EditText editText1,editText2,editText3;
    private Spinner spinner;
    private TextView tv1;
    private Youzi youzi= new Youzi();
    private String str1,str2,str3,str4,str5;
    private Button submit;
    private Map map = new HashMap();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ge_ren_xinxi);

        sharedPreferences = getSharedPreferences(\"user\",MODE_PRIVATE);
        token = sharedPreferences.getString(\"token\",\"\");
        spinner = findViewById(R.id.xinxi_sex);

        editText1 = findViewById(R.id.xinxi_et1);
        editText2 = findViewById(R.id.xinxi_et2);
        editText3 = findViewById(R.id.xinxi_et3);
        tv1 = findViewById(R.id.xinxi_username);

        //tv2 = findViewById(R.id.xinxi_sex);

        submit = findViewById(R.id.xinxi_submit);

        init();

        back = findViewById(R.id.xinxi_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    str1 = editText1.getText().toString();
                    str2 = editText2.getText().toString();
                    str3 = editText3.getText().toString();
                   String str4 = spinner.getSelectedItem().toString();
                    if (str4.equals(\"男\")){
                        str5 = \"1\";
                    }else {
                        str5 = \"0\";
                    }


                    if (!str1.isEmpty()&&str1!=null&&str1!=\"\"&&!str2.isEmpty()&&
                            str2!=null&&str2!=\"\"&&!str3.isEmpty()&&str3!=null&&str3!=\"\"&&!str5.isEmpty()&&str5!=null&&str5!=\"\"){

                        boolean flag = new IsCarnumberNO().isemail(str2);
                        boolean flag1 = new IsCarnumberNO().isphone(str3);


                        if (flag){
                            if (flag1){
                                map.put(\"nickName\",str1);
                                map.put(\"email\",str2);
                                map.put(\"phonenumber\",str3);
                                map.put(\"sex\",str5);
                                final String mapJson = JSON.toJSONString(map);
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject jsonObject = youzi.PUTxinxi(token,mapJson);
                                            final String msg = jsonObject.getString(\"msg\");
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (msg.equals(\"操作成功\")){
                                                        Toast.makeText(GeRenXinxi.this, \"修改成功\", Toast.LENGTH_SHORT).show();

                                                    }else {
                                                        Toast.makeText(GeRenXinxi.this, msg, Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                            });
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }else {
                                Toast.makeText(GeRenXinxi.this, \"请输入正确格式手机号\", Toast.LENGTH_SHORT).show();
                            }

                        }else {
                            Toast.makeText(GeRenXinxi.this, \"请输入正确邮箱格式\", Toast.LENGTH_SHORT).show();
                        }



                    }


                }
            });


    }

    private void init(){
        new  Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = youzi.GETxinxi(token);
                    JSONObject object = jsonObject.getJSONObject(\"user\");
                    str1 = object.getString(\"nickName\");
                    str2 = object.getString(\"email\");
                    str3 = object.getString(\"phonenumber\");
                    str4 = object.getString(\"userName\");
                    str5 = object.getString(\"sex\");


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (str1.equals(\"\")||str1==null||str1.isEmpty()){
                                editText1.setText(\"\");
                            }else {
                                editText1.setText(str1);
                            }

                            if (str2.equals(\"\")||str2==null||str2.isEmpty()){
                                editText2.setText(\"\");
                            }else {
                                editText2.setText(str2);
                            }

                            if (str3.equals(\"\")||str3==null||str3.isEmpty()){
                                editText3.setText(\"\");
                            }else {
                                editText3.setText(str3);
                            }

                            tv1.setText(str4);
                            if (str5.equals(\"0\")){
                                spinner.setSelection(1,true);
                            }else {
                                spinner.setSelection(0,true);
                            }

                            //tv2.setText(str6);

                        }
                    });


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

信息修改工具类

 public JSONObject PUTxinxi(String token,String json) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse(\"application/json\");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method(\"PUT\", body)
                .addHeader(\"Authorization\", token)
                .addHeader(\"Content-Type\", \"application/json\")
                .build();
        Response response = client.newCall(request).execute();
        String s = response.body().string();
        JSONObject jsonObject = new JSONObject(s);
        return jsonObject;
    }

密码修改布局




    
        
    
    

        

        

            

                

            
        

        


        
            
        

        

    
    

        

java代码

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.smartcity_c.R;
import com.example.smartcity_c.utils.Youzi;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class XiuGaiPassword extends AppCompatActivity {

    private EditText et_password1, et_password2;
    private LinearLayout lay1, lay2;
    private ImageView back;
    private Button button;
    private Youzi youzi = new Youzi();
    private SharedPreferences sharedPreferences;
    private String token;

    private String w1,w2;
    private Map map = new HashMap();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiu_gai_password);


        et_password1= findViewById(R.id.xiugai_a_et_account);
        et_password2 = findViewById(R.id.xiugai_a_et_password);
        lay1 = findViewById(R.id.xiugai_a_lay_1);
        lay2 = findViewById(R.id.xiugai_a_lay_2);
        back = findViewById(R.id.xiugai_back);
        button = findViewById(R.id.xiugai_a_submit);

        sharedPreferences = getSharedPreferences(\"user\",MODE_PRIVATE);
        token = sharedPreferences.getString(\"token\",\"\");

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        et_password1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay1.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay2.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        et_password1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    lay2.setBackgroundColor(Color.parseColor(\"#00BCD4\"));
                    lay1.setBackgroundColor(Color.parseColor(\"#E3E3E3\"));
                }
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                w1 = et_password1.getText().toString();
                w2 = et_password2.getText().toString();
                map.put(\"newPassword\",w2);
                map.put(\"oldPassword\",w1);
                final String mapjson = JSON.toJSONString(map);
                if (!w1.isEmpty()&&w1!=null&&w1!=\"\"&&!w2.isEmpty()&&w2!=null&&w2!=\"\"){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = youzi.XGpassword(mapjson,token);
                                final  String msg = jsonObject.getString(\"msg\");

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (msg.equals(\"操作成功\")){
                                            Toast.makeText(XiuGaiPassword.this, \"修改成功\", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }else {
                                            Toast.makeText(XiuGaiPassword.this, msg, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }else {
                    Toast.makeText(XiuGaiPassword.this, \"请填写信息\", Toast.LENGTH_SHORT).show();
                }


            }
        });


    }
}

工具类

public JSONObject XGpassword(String json,String token) throws Exception {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse(\"application/json\");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method(\"PUT\", body)
                .addHeader(\"Authorization\", token)
                .addHeader(\"Content-Type\", \"application/json\")
                .build();
        Response response = client.newCall(request).execute();
        String s = response.body().string();
        JSONObject jsonObject = new JSONObject(s);
        return jsonObject;
    }

主页面展示




    

        

            
        
        
            

            
            
                
                
            




        
    

    
        

主页面代码

package com.example.smartcity_c.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity_c.R;
import com.example.smartcity_c.login.DinDan;
import com.example.smartcity_c.login.GeRenXinxi;
import com.example.smartcity_c.login.Login;
import com.example.smartcity_c.login.XiuGaiPassword;

import static android.content.Context.MODE_PRIVATE;


public class Fragment5 extends Fragment {


    private TextView login;
    private TextView name;
    private Button btn1, btn2, btn3,btn4;
    private SharedPreferences sharedPreferences1;
    private SharedPreferences.Editor editor;


    private boolean flag;

    public Fragment5(boolean flag) {
        this.flag = flag;
    }

    public Fragment5() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_5, container, false);

        login = view.findViewById(R.id.f3_login);
        name = view.findViewById(R.id.f3_name);

        btn1 = view.findViewById(R.id.gr_btn1);
        btn2 = view.findViewById(R.id.gr_btn2);
        btn3 = view.findViewById(R.id.gr_btn3);
        btn4 = view.findViewById(R.id.gr_btn4);

        sharedPreferences1 = getActivity().getSharedPreferences(\"user\", MODE_PRIVATE);
        final String namee = sharedPreferences1.getString(\"username\", \"\");
        editor = sharedPreferences1.edit();


        if (flag) {
            name.setVisibility(View.VISIBLE);
            login.setVisibility(View.GONE);
            //name.setText(username);
            name.setText(namee);

            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getContext(), XiuGaiPassword.class));
                }
            });

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getContext(), GeRenXinxi.class));
                }
            });
            btn4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getContext(), DinDan.class));
                }
            });

        } else {
            name.setVisibility(View.GONE);
            login.setVisibility(View.VISIBLE);

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), Login.class));
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), Login.class));
                }
            });
            btn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), Login.class));
                }
            });
            btn4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), Login.class));
                }
            });
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), Login.class));
            }
        });


        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (namee.equals(\"\") || namee == \"\" || namee == null || namee.isEmpty()) {
                    startActivity(new Intent(getActivity(), Login.class));
                } else {
                    editor.putBoolean(\"check\", false);
                    editor.putString(\"token\", \"\");
                    editor.putString(\"username\", \"\");
                    editor.commit();
                    Toast.makeText(getActivity(), \"您已退出登录\", Toast.LENGTH_SHORT).show();
                    getActivity().finish();
                    Intent intent = new Intent(getContext(), getActivity().getClass());
                    startActivity(intent);
                }
            }
        });

        return view;
    }

}

信息校验工具类

import android.text.TextUtils;

public class IsCarnumberNO {
    public  boolean isCarnumber(String carnumber) {
   /*
   1.常规车牌号:仅允许以汉字开头,后面可录入六个字符,由大写英文字母和阿拉伯数字组成。如:粤B12345;
   2.武警车牌:允许前两位为大写英文字母,后面可录入五个或六个字符,由大写英文字母和阿拉伯数字组成,其中第三位可录汉字也可录大写英文字母及阿拉伯数字,第三位也可空,如:WJ警00081、WJ京1234J、WJ1234X。
   3.最后一个为汉字的车牌:允许以汉字开头,后面可录入六个字符,前五位字符,由大写英文字母和阿拉伯数字组成,而最后一个字符为汉字,汉字包括“挂”、“学”、“警”、“军”、“港”、“澳”。如:粤Z1234港。
   4.新军车牌:以两位为大写英文字母开头,后面以5位阿拉伯数字组成。如:BA12345。
       */
        String carnumRegex = \"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$\";
        if (TextUtils.isEmpty(carnumber)) return false;
        else return carnumber.matches(carnumRegex);


    }
    public  boolean isdata(String carnumber) {
        String regex = \"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29) \";
        if (TextUtils.isEmpty(carnumber)) return false;
        else return carnumber.matches(regex);
    }
    public boolean isphone(String carnumber){
        String check = \"^[1][3,4,5,7,8,9][0-9]{9}$\";

        if (TextUtils.isEmpty(carnumber)) return false;
        else return carnumber.matches(check);
    }
    public boolean isemail(String carnumber){
        String check = \"^([a-z0-9A-Z]+[-|\\\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\\\.)+[a-zA-Z]{2,}$\";

        if (TextUtils.isEmpty(carnumber)) return false;
        else return carnumber.matches(check);
    }
}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号