http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2862|回复: 5

[新手开发之旅] Android新手开发之旅-实现APP登录功能

[复制链接]
发表于 2018-12-30 19:45:50 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-28 14:57 编辑

本篇来说下如何实现APP的登录功能

我们此次使用第三方开源框架xUtil进行举例,不了解的可以百度下,这里就不再多说


1、在gradle中添加xUtil依赖 compile 'org.xutils:xutils:3.5.0'

         QQ截图20181228113125.png


2、新建MyApplication
[Java] 纯文本查看 复制代码
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        //初始化
        x.Ext.init(this);

        // 是否输出debug日志, 开启debug会影响性能.
        x.Ext.setDebug(BuildConfig.DEBUG);
    }
}


3、在AndroidManifest.xml设置新建的MyApplication


         QQ截图20181228114221.png

4、因为要从服务器请求数据,所以还要在AndroidManifest.xml添加权限


         QQ截图20181228114418.png

5、准备工作已经做好了,开始代码部分,这里有两个类MainActivity(主页面)和SecondActivity(用来显示登录成功后的用户信息)
activity_main.xml:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_account"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@android:color/transparent"
            android:hint="请输入账号"
            android:textSize="20sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimaryDark" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:background="@android:color/transparent"
            android:hint="请输入密码"
            android:textSize="20sp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimaryDark" />

    <TextView
        android:id="@+id/tv_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/bg_login"
        android:gravity="center"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</LinearLayout>


activity_second.xml:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="头像"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形头像"
        android:textColor="@android:color/black"
        android:textSize="16sp" />
</LinearLayout>

MainActivity:
[Java] 纯文本查看 复制代码
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    //初始化控件
    @ViewInject(R.id.editText_account)
    private EditText editText_account;
    @ViewInject(R.id.editText_password)
    private EditText editText_password;
    @ViewInject(R.id.tv_login)
    private TextView tv_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        x.view().inject(this);
    }

    //登录按钮点击事件
    @Event(value = R.id.tv_login)
    private void tv_login(View view) {
        if (TextUtils.isEmpty(editText_account.getText())) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(editText_password.getText())) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
        } else {
            login();
        }
    }

    private void login() {
        RequestParams params = new RequestParams("填写自己的请求地址");
        params.addBodyParameter("action", "pwdLogin");
        params.addBodyParameter("mobile", editText_account.getText().toString());
        params.addBodyParameter("pwd", editText_password.getText().toString());
        params.addBodyParameter("from", Build.MODEL);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //result就是请求到的数据
                Log.i("TAG", result);
                try {
                    //解析数据
                    JSONObject object = new JSONObject(result);
                    Toast.makeText(MainActivity.this, object.getString("Content"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("Status") == 1) {
                        JSONObject returnValue = object.getJSONObject("ReturnValue");
                        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                        intent.putExtra("name", returnValue.getString("UserName"));
                        intent.putExtra("url", returnValue.getString("LogoUrl"));
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("TAG", ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}

secondActivity:
[Java] 纯文本查看 复制代码
@ContentView(R.layout.activity_second)
public class SecondActivity extends AppCompatActivity {
    @ViewInject(R.id.tv)
    private TextView tv;
    @ViewInject(R.id.iv)
    private ImageView iv;
    @ViewInject(R.id.iv2)
    private ImageView iv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        x.view().inject(this);
        setView();
    }

    private void setView() {
        String name = getIntent().getStringExtra("name");
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(name)) {
            tv.setText("获取到的名字--------" + name);
        }
        if (!TextUtils.isEmpty(url)) {
            //使用xUtil显示图片
            x.image().bind(iv, url);

            // 渐变效果
            ImageOptions imageOptions = new ImageOptions.Builder().setFadeIn(true)
                    // 加载中或错误图片的ScaleType
                    .setPlaceholderScaleType(ImageView.ScaleType.CENTER_CROP)
                    .setLoadingDrawableId(R.mipmap.ic_launcher)// 加载中图片
                    .setFailureDrawableId(R.mipmap.ic_launcher)// 加载失败图片
                    .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    //是否显示成圆形
                    .setCircular(true).build();
            x.image().bind(iv2, url, imageOptions);
        }
    }
}


下面分别是登录失败和成功的两种数据:
  
         QQ截图20181228144402.png


效果图:


         IMG_20181228_144916.JPG
      
                   Screenshot_20181228_145130.png
           
      

                     这样就完成了一个简单的登录功能













1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-30 19:48:19 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-30 21:03:28 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-30 21:46:56 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-30 22:03:18 | 显示全部楼层
看到这帖子真是高兴!
发表于 2019-1-2 20:13:55 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-18 09:54

© 2014-2021

快速回复 返回顶部 返回列表