android material design之简单控件(八)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23195583/article/details/51770475

我的项目源码托管地址:点击打开我的项目源码地址


这算是md风格这一段时间的最后一篇博客吧,以后也可能会更新,看几个图


先来看edittext的activity

package com.fanyafeng.materialdesign.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.fanyafeng.materialdesign.BaseActivity;
import com.fanyafeng.materialdesign.R;

public class EditTextFloatingLabelActivity extends BaseActivity {
    private TextInputLayout layoutEtEmail;
    private EditText etEmail;

    private TextInputLayout layoutEtPwd;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text_floating_label);

        title = "测试floatinglabel";

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        initView();
        initData();
    }

    private void initView() {
        layoutEtEmail = (TextInputLayout) findViewById(R.id.layoutEtEmail);
        etEmail = (EditText) findViewById(R.id.etEmail);

        layoutEtPwd = (TextInputLayout) findViewById(R.id.layoutEtPwd);
        etPwd = (EditText) findViewById(R.id.etPwd);

    }

    private void initData() {
        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = etEmail.getText().toString().trim();
                if (!text.equals("") && text != null) {
                    layoutEtEmail.setErrorEnabled(false);
                    layoutEtEmail.setError(null);
                } else {
                    layoutEtEmail.setErrorEnabled(true);
                    layoutEtEmail.setError("格式错误");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


//        layoutEtPwd.setErrorEnabled(true);
//        layoutEtPwd.setError("密码为数字");
    }

}


两个xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.fanyafeng.materialdesign.activity.EditTextFloatingLabelActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_edit_text_floating_label" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.fanyafeng.materialdesign.activity.EditTextFloatingLabelActivity"
    tools:showIn="@layout/activity_edit_text_floating_label">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layoutEtEmail"
            app:theme="@style/AppTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/etEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入邮箱"
                android:inputType="textEmailAddress" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/layoutEtPwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/etPwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="请输入密码"
                android:inputType="numberPassword" />

        </android.support.design.widget.TextInputLayout>


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>


另个的xml,其他的代码就不贴了

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.fanyafeng.materialdesign.activity.V7WidgetActivity"
    tools:showIn="@layout/activity_v7_widget">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号" />

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码" />

        <android.support.design.widget.VisibilityAwareImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/background" />

        <android.support.v7.widget.AppCompatSpinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/spinnername" />

        <android.support.v7.widget.SearchView
            android:layout_width="match_parent"
            android:layout_height="56dp" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

写给自己:

进步处便思退步,庶免触藩之祸;

著手时先图放手,才脱骑虎之危。


阅读更多
换一批

没有更多推荐了,返回首页