Android实现简洁的APP登录界面

所属分类: 软件编程 / Android 阅读数: 40
收藏 0 赞 0 分享

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

先来看下效果图

1.布局的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#2197db"
 >
 <ImageView
  android:id="@+id/loginbutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="40dp"
  android:src="@drawable/login_pic"/>
 
<LinearLayout
  android:id="@+id/input"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/loginbutton"
  android:layout_marginLeft="28dp"
  android:layout_marginRight="28dp"
  android:background="#fff"
  android:orientation="vertical">
<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="44dp"
  android:background="#fff"
  android:gravity="center_vertical"
  android:orientation="horizontal" >

 <EditText
   android:id="@+id/userId"
   android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   android:layout_weight="1"
   android:background="@null"
   android:imeOptions="actionDone"
   android:textSize="16sp"
   android:ems="10"
   android:hint="请输入用户名"
   >
 </EditText>
<Button
   android:id="@+id/button_bar"
   android:layout_width="20dp"
   android:layout_height="20dp"
   android:layout_marginRight="8dp"
   android:layout_marginLeft="1dp"
   android:background="@drawable/login_input_arrow"
   />

 </LinearLayout>
 <View
   android:layout_width="fill_parent"
   android:layout_height="1.0px"
   android:layout_marginLeft="1.0px"
   android:layout_marginRight="1.0px"
   android:background="#ffc0c3c4" />
<EditText
   android:id="@+id/pass"
   android:layout_width="fill_parent"
   android:layout_height="44.0dip"
   android:background="#00ffffff"
   android:gravity="center_vertical"
   android:inputType="textPassword"
   android:maxLength="16"
   android:maxLines="1"
   android:textColor="#ff1d1d1d"
   android:textColorHint="#ff666666"
   android:textSize="16.0sp"
   android:hint="请输入密码"
   />
 </LinearLayout>
 <Button
  android:id="@+id/loginBtn"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/input"
  android:layout_marginTop="10dp"
  android:background="#3aadfd"
  android:text="登 录"
  android:textColor="#ffffff"
  android:textSize="18dp"
  android:layout_centerHorizontal="true"
  android:layout_marginLeft="28dp"
  android:layout_marginRight="28dp"/>
 <TextView
  android:text=""
  android:layout_width="wrap_content"
  android:layout_below="@+id/loginBtn"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:id="@+id/promptText"
  android:textColor="#ff0000"
  android:layout_marginTop="10dp"
  android:textSize="18sp"/>

</RelativeLayout>

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();
      }
     });

 }
}  

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

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

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多