利用Android怎么制作一个APP登录界面

发布时间:2020-12-02 17:09:10 作者:Leah
来源:亿速云 阅读:287

这期内容当中小编将会给大家带来有关利用Android怎么制作一个APP登录界面,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1.布局的xml文件

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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+"&#63;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怎么制作一个APP登录界面了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. C#如何制作登录界面
  2. Android静默拍摄app制作方法是什么?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android pp

上一篇:利用JavaMail怎么实现一个邮件发送功能

下一篇:如何在Android项目中创建一个自定义控件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》