Button的使用
Button是和用户进行交互的一个重要控件,它的属性和TextView差不多
<Button
android:id="@+id/bt_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"
android:textSize="18sp"
android:background="#000000"
android:textColor="#FFFFFF"/>
属性都比较简单,就不一一介绍了。
xml写完后,我们可以给button添加一个点击事件
Button button = findViewById(R.id.bt_hello);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//在这里进行点击事件的操作
}
});
照样,我们先通过findViewById获取到控件,再调用button 的setOnClickListener方法添加一个点击事件。