Android TextInputLayout更好看的输入框加EvenBus传值

来源:互联网 发布:阿里云首席科学家 年薪 编辑:程序博客网 时间:2024/05/17 04:44

导入依赖

compile 'com.android.support:design:22.2.0'compile 'org.greenrobot:eventbus:3.0.0'

效果图

这里写图片描述

XML布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"    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"    android:orientation="vertical"    tools:context="com.example.am20171107.MainActivity">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0.5"        android:orientation="vertical">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:src="@mipmap/umeng_socialize_qq"/>    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="0.5"        android:orientation="vertical">        <android.support.design.widget.TextInputLayout            android:id="@+id/usernameWrapper"            android:layout_width="match_parent"            android:layout_height="wrap_content">            <EditText                android:id="@+id/username"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textEmailAddress"                android:hint="Username"/>        </android.support.design.widget.TextInputLayout>        <android.support.design.widget.TextInputLayout            android:id="@+id/passwordWrapper"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/usernameWrapper"            android:layout_marginTop="4dp">            <EditText                android:id="@+id/password"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:inputType="textPassword"                android:hint="Password"/>        </android.support.design.widget.TextInputLayout>        <Button            android:id="@+id/btn"            android:layout_marginTop="4dp"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Login"/>    </LinearLayout></LinearLayout>

MainActivity

private Button btn;    private static final String EMAIL_PATTERN = "^[a-zA-Z0-9#_~!$&'()*+,;=:.\"(),:;<>@\\[\\]\\\\]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$";    private Pattern pattern = Pattern.compile(EMAIL_PATTERN);    private Matcher matcher;    public boolean validateEmail(String email) {        matcher = pattern.matcher(email);        return matcher.matches();    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper);        final TextInputLayout passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper);        usernameWrapper.setHint("Username");        passwordWrapper.setHint("Password");        btn= (Button) findViewById(R.id.btn);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                hideKeyboard();                String username = usernameWrapper.getEditText().getText().toString();                String password = passwordWrapper.getEditText().getText().toString();                if (!validateEmail(username)) {                    usernameWrapper.setError("Not a valid email address!");                } else if (!validatePassword(password)) {                    passwordWrapper.setError("Not a valid password!");                } else {                    usernameWrapper.setErrorEnabled(false);                    passwordWrapper.setErrorEnabled(false);                    EventBus.getDefault().postSticky(new FirstEvent(username,password));                    startActivity(new Intent(MainActivity.this,LoginActivity.class));                    doLogin();                }            }        });    }    private void hideKeyboard() {        View view = getCurrentFocus();        if (view != null) {            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);        }    }    public boolean validatePassword(String password) {        return password.length() > 5;    }    public void doLogin() {        Toast.makeText(getApplicationContext(), "OK! I'm performing login.", Toast.LENGTH_SHORT).show();        // TODO: login procedure; not within the scope of this tutorial.    }

显示Evenbus传值的activity

private TextView t1;    private TextView t2;    private Button send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        t1= (TextView) findViewById(R.id.t1);        t2= (TextView) findViewById(R.id.t2);        send= (Button) findViewById(R.id.send);        send.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                EventBus.getDefault().register(LoginActivity.this);            }        });    }    @Subscribe(sticky = true)    public void onEventMainThread(FirstEvent event) {        String name = event.getName();        String password = event.getPassword();        t1.setText(name);        t2.setText(password);    }    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }
原创粉丝点击