java app 登录界面设计_Android实现简洁的APP登录界面

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

先来看下效果图

74e8857cd19e9c3e122a249c380a9c58.png

1.布局的xml文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#2197db"

>

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

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

android:layout_width="fill_parent"

android:layout_height="44dp"

android:background="#fff"

android:gravity="center_vertical"

android:orientation="horizontal" >

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="请输入用户名"

>

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"

/>

android:layout_width="fill_parent"

android:layout_height="1.0px"

android:layout_marginLeft="1.0px"

android:layout_marginRight="1.0px"

android:background="#ffc0c3c4" />

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="请输入密码"

/>

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

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

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

}

});

}

}

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值