Android中登录和注册的功能和布局展示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hongfuqiang/article/details/78428144

展示的效果图:



源代码如下:

首先要导入依赖

compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'org.xutils:xutils:3.5.0'
compile files('libs/gson-2.2.4.jar')
还有就是 注册网络

<uses-permission android:name="android.permission.INTERNET" />
android:name=".MyApplication"

接下来布局

主布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/dl"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#000"
        android:layout_below="@+id/dl"
        android:id="@+id/xian"
        />
    <EditText
        android:hint="情输入手机号"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/shoujihao"
        android:inputType="number"
        android:layout_alignTop="@+id/xian"
        android:layout_alignParentStart="true" />
    <EditText
        android:hint="情输入密码"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/mima"
        android:inputType="number"
        android:layout_below="@+id/shoujihao"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/button0"
        android:layout_below="@+id/mima"
        android:layout_toStartOf="@+id/dl" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/button1"
        android:layout_alignTop="@+id/button0"
        android:layout_toEndOf="@+id/dl" />
</RelativeLayout>

第二个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/dl"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#000"
        android:layout_below="@+id/dl"
        android:id="@+id/xian"
        />
    <EditText
        android:hint="情输入手机号"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/zcsjh"
        android:inputType="number"
        android:layout_alignTop="@+id/xian"
        android:layout_alignParentStart="true" />
    <EditText
        android:hint="情输入密码"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/zcmima"
        android:inputType="number"
        android:layout_below="@+id/zcsjh"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/butzhuce"
        android:layout_below="@+id/zcmima"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp" />
</RelativeLayout>
第三个布局:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

</RelativeLayout>
接下来就是各种类了

登录类: 

public class LoginBean {
    private String msg;
    private String code;


    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "LoginBean{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                '}';
    }
}

注册类:

public class RegistBean {
    private String msg;
    private String code;
    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
定义自定义的myApplication

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
    }
}


主main页面

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button denglu;
    private Button zhuce;
    private EditText shoujihao;
    private EditText mima;
    private String sjh;
    private String mm;
    String num="[1][358]\\d{9}";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        denglu = (Button) findViewById(R.id.button0);
        zhuce = (Button) findViewById(R.id.button1);
        shoujihao = (EditText) findViewById(R.id.shoujihao);
        mima = (EditText) findViewById(R.id.mima);
        setOnClick();
    }

    private void setOnClick() {
        denglu.setOnClickListener(this);
        zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        sjh=shoujihao.getText().toString().trim();
        mm=mima.getText().toString().trim();
        switch (view.getId()){
            case R.id.button0:
                boolean flag=checkData(sjh,mm);
                if(flag){
                    register(sjh,mm);
                }
                break;
            case R.id.button1:
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;
        }
    }
    private void register(String sjh, String mm) {
        final RequestParams params=new RequestParams("http://120.27.23.105/user/login");
        params.addQueryStringParameter("mobile",sjh);
        params.addQueryStringParameter("password",mm);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                LoginBean bean = gson.fromJson(result, LoginBean.class);
                Toast.makeText(MainActivity.this,bean.getMsg().toString(),Toast.LENGTH_SHORT).show();
                if(bean.getCode().equals("0")){
                    Toast.makeText(MainActivity.this,"已成功登陆",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(MainActivity.this,Main3Activity.class);
                    startActivity(intent);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private boolean checkData(String sjh, String mm) {
        if(TextUtils.isEmpty(sjh)||TextUtils.isEmpty(mm)){
            Toast.makeText(this,"手机号和密码不能为空",Toast.LENGTH_LONG).show();
            return false;
        } else if(mm.length()<6){
            Toast.makeText(this,"密码不能小于6位",Toast.LENGTH_LONG).show();
            return false;
        }else {
            return sjh.matches(num);
        }

    }


}
 第二个main页面

public class Main2Activity extends AppCompatActivity {

    private String sjh;
    private String mm;
    private Button butzhuce;
    private EditText zcsjh;
    private EditText zcmima;
    private String num="[1][358]\\d{9}";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        butzhuce = (Button) findViewById(R.id.butzhuce);
        zcsjh = (EditText) findViewById(R.id.zcsjh);
        zcmima = (EditText) findViewById(R.id.zcmima);
        sjh= zcsjh.getText().toString();
        mm= zcmima.getText().toString();

        butzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sjh= zcsjh.getText().toString();
                mm= zcmima.getText().toString();
                boolean a=checkData(sjh,mm);
                if(a==true){
                    Toast.makeText(Main2Activity.this,"可以",Toast.LENGTH_LONG).show();
                    regfster(sjh,mm);
                }else{
                    Toast.makeText(Main2Activity.this,"手机不合法",Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    private void regfster(String sjh, String mm) {
        RequestParams params = new RequestParams("http://120.27.23.105/user/reg");
        params.addBodyParameter("mobile",sjh);
        params.addBodyParameter("password",mm);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                RegistBean bean = gson.fromJson(result, RegistBean.class);
                Toast.makeText(Main2Activity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                if(bean.getCode().equals("0")){
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
第三个main页面

public class Main3Activity extends AppCompatActivity {

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





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页