MainActivity中的代码
package com.example.constraintlayouttest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; private CheckBox checkBox; private EditText editText1; public MainActivity() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) findViewById(R.id.button5); editText=(EditText)findViewById(R.id.edit_text); checkBox=(CheckBox) findViewById(R.id.check_box); editText1=(EditText)findViewById(R.id.edit_text1); button.setOnClickListener(this); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton mCompoundButton, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { editText1.setTransformationMethod(null); }else editText1.setTransformationMethod(PasswordTransformationMethod.getInstance()); } }); } @Override public void onClick(View v){ switch (v.getId()){ case R.id.button5: String inputText="welcome "+editText.getText().toString()+"!"; Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show(); break; default: break; } } }
activity_main.xml中的代码
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="com.example.constraintlayouttest.MainActivity" tools:layout_editor_absoluteY="81dp"> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="36dp" android:text="登陆" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="@+id/guideline" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="36dp" android:layout_marginRight="4dp" android:text="注册" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.5" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" app:layout_constraintBaseline_toBaselineOf="@+id/edit_text" app:layout_constraintEnd_toEndOf="@+id/textView2" app:layout_constraintStart_toStartOf="@+id/textView2" tools:text="账户" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="232dp" android:layout_marginStart="32dp" android:text="密码" app:layout_constraintBaseline_toBaselineOf="@+id/edit_text1" app:layout_constraintEnd_toStartOf="@+id/checkBox1" app:layout_constraintStart_toStartOf="parent" tools:text="密码" /> <EditText android:id="@+id/edit_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginRight="16dp" android:layout_marginStart="8dp" android:layout_marginTop="192dp" android:hint="请输入您的账号" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/edit_text1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:hint="请输入您的密码" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/edit_text" app:layout_constraintVertical_bias="0.113" /> <CheckBox android:id="@+id/check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="12dp" android:checked="false" android:text="显示密码" app:layout_constraintStart_toStartOf="@+id/edit_text1" app:layout_constraintTop_toBottomOf="@+id/edit_text1" /> </android.support.constraint.ConstraintLayout>
其中布局采用
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.0.0-beta1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' }
运行结果