android论坛功能开发,Android新手开发之旅-实现APP登录功能

本帖最后由 liu 于 2018-12-28 14:57 编辑

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

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

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

22f0779a1bf6f8432985e5cddd2774b8.gif

QQ截图20181228113125.png (26.07 KB, 下载次数: 9)

2018-12-28 11:31 上传

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

22f0779a1bf6f8432985e5cddd2774b8.gif

QQ截图20181228114221.png (18.32 KB, 下载次数: 8)

2018-12-28 11:42 上传

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

22f0779a1bf6f8432985e5cddd2774b8.gif

QQ截图20181228114418.png (14.81 KB, 下载次数: 8)

2018-12-28 11:44 上传

5、准备工作已经做好了,开始代码部分,这里有两个类MainActivity(主页面)和SecondActivity(用来显示登录成功后的用户信息)

activity_main.xml:

[XML] 纯文本查看 复制代码

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="20dp">

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号"

android:textColor="@android:color/black"

android:textSize="20sp" />

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" />

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/colorPrimaryDark" />

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码"

android:textColor="@android:color/black"

android:textSize="20sp" />

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" />

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/colorPrimaryDark" />

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" />

activity_second.xml:

[XML] 纯文本查看 复制代码

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@android:color/black"

android:textSize="16sp" />

android:id="@+id/iv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="头像"

android:textColor="@android:color/black"

android:textSize="16sp" />

android:id="@+id/iv2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="圆形头像"

android:textColor="@android:color/black"

android:textSize="16sp" />

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() {

@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);

}

}

}

下面分别是登录失败和成功的两种数据:

22f0779a1bf6f8432985e5cddd2774b8.gif

QQ截图20181228144402.png (17.25 KB, 下载次数: 8)

2018-12-28 14:44 上传

效果图:

22f0779a1bf6f8432985e5cddd2774b8.gif

IMG_20181228_144916.JPG (169.16 KB, 下载次数: 12)

2018-12-28 14:49 上传

22f0779a1bf6f8432985e5cddd2774b8.gif

Screenshot_20181228_145130.png (114.56 KB, 下载次数: 10)

2018-12-28 14:51 上传

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部