详解Android Studio实现用户登陆界面demo(xml实现)

 更新时间:2020年05月13日 11:29:17   作者:Wendy__ZYX  
这篇文章主要介绍了详解Android Studio实现用户登陆界面demo,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

使用Android Studio 编写的第一个demo,使用布局文件—xml实现用户登录界面

注:所建工程均为Android 6.0 所以只要是Android 6.0(包括6.0)以上的真机,模拟机都可以使用

Step1:Android Studio 开发环境的搭建:

1.安装JDK (1.8);
2.安装Android studio (3.3.1) 包含 gradle、sdk manage 、avd manage ;
3.使用sdk manage 下载安装 sdk;
4.使用avd manages 创建虚拟机

Step2: 新建工程项目Myapp2.0

1.在res/layout/activity_main.xml中编写布局内容:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingLeft="55px"
  android:paddingRight="50px"
  tools:context=".MainActivity">
  <TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="Hello Word!"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  <View
    android:layout_width="match_parent"
    android:layout_height="2px"
    android:layout_marginTop="16px"
    android:background="#000000" />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20px"
    android:text="登陆界面" />
<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  >
  <EditText
    android:id="@+id/et1"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:paddingLeft="10dp"
    android:hint="请输入账号"
    android:inputType="text"/>
  <ImageView
    android:id="@+id/bt1"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_marginTop="37dp"
    android:src="@drawable/delete" />
</LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="8px">
  <EditText
    android:id="@+id/et2"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    android:hint="请输入密码"
    android:inputType="textPassword" />
    <ImageView
      android:id="@+id/bt2"
      android:layout_width="25dp"
      android:layout_height="25dp"
      android:layout_marginTop="33dp"
      android:src="@drawable/delete" />
</LinearLayout>
    <Button
      android:id="@+id/btn_login"
      android:layout_width="match_parent"
      android:layout_height="48dp"
      android:background="@color/bbutton_danger_disabled_edge"
      android:layout_marginTop="30dp"
      android:text="登 陆"
      android:textSize="30dp"
      android:textColor="@color/bbutton_danger"/>
  <Button
  android:id="@+id/bbt1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="15dp"
  android:layout_gravity="right"
  android:layout_marginTop="20px"
  android:background="@color/bbutton_danger"
  android:text="Adapter" />
  </LinearLayout> 

2.创建一个Java class —ExitTextUtils用于封装清空输入框的内容 :

/**
 * 用于实现点击叉叉时 , 清空输入框的内容
 */

 class EditTextUtils {

  public static void clearButtonListener(final EditText et, final View view) {

    // 取得et中的文字

    String etInputString = et.getText().toString();

    // 根据et中是否有文字进行X可见或不可见的判断

    if (TextUtils.isEmpty(etInputString)) {

      view.setVisibility(View.INVISIBLE);
    } else {

      view.setVisibility(View.VISIBLE);
    }

    //点击X时使et中的内容为空

    view.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        et.setText("");
        et.requestFocusFromTouch();
      }
    });
    //对et的输入状态进行监听
    et.addTextChangedListener(new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
      }
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }
      @Override
      public void afterTextChanged(Editable s) {

        if (s.length() == 0) {
          view.setVisibility(View.INVISIBLE);
        } else {
          view.setVisibility(View.VISIBLE);
        }
      }
    });
  }
}

3.在MainActivity.java 里书写代码:

private TextView mTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  EditText et1 = (EditText) findViewById(R.id.et1);
  EditText et2 = (EditText) findViewById(R.id.et2);
  View bt = findViewById(R.id.bt1);
  View iv = findViewById(R.id.bt2);
  EditTextUtils.clearButtonListener(et1, bt);
  EditTextUtils.clearButtonListener(et2, iv);

  Button btn1 = (Button) findViewById(R.id.bbt1);
  btn1.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
      //Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件,在存放资源代码的文件夹下下,
      Intent i = new Intent(MainActivity.this , Main2ActivityAdapterDemo.class);
      //启动
      startActivity(i);
      }
  });

  mTextMessage = (TextView) findViewById(R.id.message);
  BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

4.布局使用到的资源:

自己建的用于存放自定义的文件 dimens.xml

<resources>
  <!-- Default screen margins, per the Android Design guidelines. -->
  <dimen name="activity_horizontal_margin">16dp</dimen>
  <dimen name="activity_vertical_margin">16dp</dimen>
  <dimen name="text_size_16">22dp</dimen>
  <dimen name="space_8">8</dimen>
  <dimen name="space_16">16</dimen>
  <dimen name="fab_margin">16dp</dimen>
</resources>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="colorPrimary">#008577</color>
  <color name="colorPrimaryDark">#00574B</color>
  <color name="colorAccent">#D81B60</color>
  <color name="main_gray">#CCCCCC</color>
  <color name="main_black">#000000</color>
  <color name="bbutton_danger_disabled_edge">#00CC33</color>
  <color name="bbutton_danger">#FFFFFF</color>
</resources>

截图

Step3:运行程序。。。截图如下:

下载地址:[LoginDemo.zip]

到此这篇关于详解Android Studio实现用户登陆界面demo(xml实现)的文章就介绍到这了,更多相关Android Studio用户登陆内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android 实现背景图和状态栏融合方法

    Android 实现背景图和状态栏融合方法

    下面小编就为大家分享一篇Android 实现背景图和状态栏融合方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Android第三方HTTP网络支持包OkHttp的基础使用教程

    Android第三方HTTP网络支持包OkHttp的基础使用教程

    在GitHub上开源的安卓HTTP编程包OkHttp正在积累着越来越高的人气,这里我们就来看一下这款Android第三方HTTP网络支持包OkHttp的基础使用教程:
    2016-07-07
  • Android中Socket大文件断点上传示例

    Android中Socket大文件断点上传示例

    本篇文章主要介绍了Android中Socket大文件断点上传示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Android进阶教程之ViewGroup自定义布局

    Android进阶教程之ViewGroup自定义布局

    这篇文章主要给大家介绍了关于Android进阶教程之ViewGroup自定义布局的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • android ItemTouchHelper实现可拖拽和侧滑的列表的示例代码

    android ItemTouchHelper实现可拖拽和侧滑的列表的示例代码

    本篇文章主要介绍了ItemTouchHelper实现可拖拽和侧滑的列表的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android在自定义类中实现自定义监听器方式

    Android在自定义类中实现自定义监听器方式

    这篇文章主要介绍了Android在自定义类中实现自定义监听器方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android仿主流壁纸App设置界面

    Android仿主流壁纸App设置界面

    这篇文章主要为大家详细介绍了Android仿主流壁纸App壁纸设置界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android UI开发中所遇到的各种坑

    Android UI开发中所遇到的各种坑

    今天小编就为大家分享一篇关于Android UI开发中所遇到的各种坑,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android 加载GIF图最佳实践方案

    Android 加载GIF图最佳实践方案

    最近在项目中遇到需要在界面上显示一个本地的 GIF 图的功能,下面通过本文给大家分享Android 加载GIF图最佳实践方案,需要的朋友参考下吧
    2017-08-08
  • Android百度地图定位、显示用户当前位置

    Android百度地图定位、显示用户当前位置

    这篇文章主要为大家详细介绍了Android百度地图定位、显示用户当前位置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01

最新评论