栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现简洁的APP登录界面

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现简洁的APP登录界面

今天需求要做一个所有app都有的登录界面,正好巩固一下我们之前学的基础布局知识。

先来看下效果图

1.布局的xml文件



 
 



 
 

2.java部分代码

public class LoginActivity extends Activity implements View.OnClickListener{
  private static final String TAG = "login";
   Button loginBtn = null;
   EditText useridEt = null;
   EditText passEt = null;
   TextView promptText = null;
   @Override
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_login);
  loginBtn = (Button) findViewById(R.id.loginBtn);
  loginBtn.setonClickListener(this);
  useridEt = (EditText) findViewById(R.id.userId); 
  passEt = (EditText) findViewById(R.id.pass);
  promptText = (TextView) findViewById(R.id.promptText);
  OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .connectTimeout(10000L, TimeUnit.MILLISECONDS)
    .readTimeout(10000L, TimeUnit.MILLISECONDS)
    .build();
  OkHttpUtils.initClient(okHttpClient);

 @Override
 public void onClick(View v) {
  String userid = useridEt.getText().toString().trim();
  String pass = passEt.getText().toString().trim();
  if(userid.equals("")){
   promptText.setText(R.string.userIdError);
   return ;
  }
  if(pass.equals("")){
   promptText.setText(R.string.passError);
   return ;
  }
 WebConstant.digest = ("Basic " + new String(base64.encode((userid + ':' + pass).getBytes(), base64.DEFAULT))).replace("n", "");

   String url = WebConstant.REQUESTPATH+"/users/" + userid+"?getAll=true";
   OkHttpUtils.get()
     .url(url).addHeader("Authorization", WebConstant.digest).addHeader("Accept-Language","zh-CN")
     .build().execute(new Callback()
     {
      @Override
      public String parseNetworkResponse(Response response, int id) throws Exception {
String string = response.body().string();
JSonObject jsonObj = new JSonObject(string);
if(jsonObj.get("userName")!=null){
 WebConstant.userId = (String)jsonObj.get("userId");
 WebConstant.userName = (String)jsonObj.get("userName");
 return (String) jsonObj.get("userName");
}
return null;
      }

      @Override
      public void onError(Call call, Exception e, int id) {
WebConstant.digest = null;
promptText.setText(R.string.loginError);
Log.i(TAG,e.getMessage());
e.printStackTrace();
      }

      @Override
      public void onResponse(Object response, int id) {
promptText.setText(R.string.loginSuccess+" "+response);
Intent intent = new Intent();
LoginActivity.this.setResult(WebConstant.RESULT_OK, intent);
LoginActivity.this.finish();
      }
     });

 }
}  

简单的登录,用户名密码验证。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/159065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号