因为这两个控件都是集成TextView的,所以TextView有的属性,它们两个都有。放在一起说,
一、Button控件
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
运行你看,控件背景色没有修改。这下我们做下修改。打开res文件下的values文件夹下的themes.xml的style加多Bridge改为如下:
<style name="Theme.MyAndroidApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
values-night文件夹下的themes.xml也要做同样的处理
1、android:id
给当前控件定义一个唯一的标识符。这样在java代码那边可以通过这id find出控件。
建议命名规则:控件的缩写加下划线,如Button的btn_xx,TextView的tv_xx,ListView的lv_xx
2、加点击事件
加setOnClickListener点击监听
如下示例代码:
public class ButtonActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
button = findViewById(R.id.btn_submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("onClick","点击了按钮");
}
});
}
}
二、EditText控件
直接看下面的输入框怎么实现:

<EditText
android:id="@+id/et_desc"
android:layout_width="match_parent"
android:layout_height="@dimen/d500px"
android:layout_margin="@dimen/d24px"
android:background="@drawable/bg_c_f5_corner_6"
android:gravity="top|left"
android:hint="@string/introduction_desc"
android:maxLength="500"
android:padding="@dimen/d26px"
android:textSize="@dimen/s32px" />
1、android:hint:默认提示语
2、默认的输入框背景比较丑,修改
第一个方式不要背景:android:background="@null"
第二个方式设置自己的背景android:background="@drawable/bg_c_f5_corner_6"
3、android:inputType:限制输入的是数字还是汉子,明文还是密文
如数字:设为"number"
如手机号:设为"phone"
如密文:设为"textPassword"
动态切换明文与密文:
//显示
mEtPwd.setTransformationMethod(new HideReturnsTransformationMethod());
//隐藏
mEtPwd.setTransformationMethod( new PasswordTransformationMethod());
网友评论