- 常用属性
- 监听事件
- 制作登录界面
activity_main.xml
<Button
android:id="@+id/btn_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:textAllCaps="false"/>
MainActivity.java
private Button mBtnEditText;
mBtnEditText=findViewById(R.id.btn_edittext);
mBtnEditText.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//跳转到EditText演示界面
Intent intent = new Intent(MainActivity.this,EditTextActivity.class);
startActivity(intent);
}
});
activity_edittext.xml
<?xml version="1.0" encoding="utf-8"?><EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:hint="用户名"
android:background="@drawable/bg_edittext"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawableLeft="@drawable/icon_user02"
android:drawablePadding="5dp"
android:maxLines="1"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/et_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="#FFAD33"
android:inputType="textPassword"
android:hint="密码"
android:layout_below="@id/et_1"
android:layout_marginTop="15dp"
android:background="@drawable/bg_edittext"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawableLeft="@drawable/icon_password"
android:drawablePadding="5dp"
android:maxLines="1"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/et_2"
android:layout_marginTop="40dp"
android:text="登录"
android:textColor="#fff"
android:textSize="20sp"
android:background="@drawable/bg_btn4"/>
EditTextActivity.java
package cy.com.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextActivity extends AppCompatActivity {
private Button mBtnLogin;
private EditText mEtUserName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
mBtnLogin = findViewById(R.id.btn_login);
mBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(EditTextActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});
mEtUserName = findViewById(R.id.et_1);
mEtUserName.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) {
Log.d("edittext",s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}