TextInputLayout使用:创建一个登陆界面
2017-08-07 15:50
393 查看
build.gradle:
MainActivity:
xml:
compile 'com.android.support:appcompat-v7:26.0.0-alpha1' compile 'com.android.support:design:25.2.0'
MainActivity:
public class MainActivity extends ActionBarActivity { TextInputLayout usernameWrapper; TextInputLayout passwordWrapper; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper); passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper); 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 = usernameWrapper.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); doLogin(); } } }); } private void hideKeyboard() { View view = getCurrentFocus(); if (view != null) { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)). hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } 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(); } 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. } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e3e3e3" android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="Welcome" android:textColor="#333333" android:textSize="30sp" /> </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_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:text="Login" /> </LinearLayout> </LinearLayout>
相关文章推荐
- 使用TextInputLayout创建一个登陆界面
- Anroid 新特性 使用TextInputLayout的使用 :创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面(j界面很漂亮)
- 使用TextInputLayout创建一个登陆界面(转自google官网)
- (转)使用TextInputLayout创建一个登陆界面
- Android进阶UI之使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- Android 使用TextInputLayout创建一个登陆界面
- android 使用TextInputLayout创建一个登陆界面
- Android 即时通讯 使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- 使用TextInputLayout创建一个登陆界面
- Android使用TextInputLayout创建登陆页面
- 1.Animation的使用 - 使用动画创建一个不同的登陆界面
- TextInputLayout+EditText在注册界面的使用(密码的的隐藏和可见,imeOptions的使用)
- 2.Animation的使用 - 创建一个可以自动切换界面的游戏登陆页面
- 从登陆界面学习TextInputLayout