控件 - Button

2019-04-24  本文已影响0人  C_G__

Button 按钮

按钮

属性


示例代码

activity_com_button.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=".chapter03.ButtonActivity">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is a Button!"
                android:textSize="24sp"
                android:textColor="#ccffff"
                android:gravity="center"
                android:shadowColor="#000000"
                android:shadowRadius="5"
                android:shadowDy="3"
                android:shadowDx="3"
                android:textAllCaps="false"/>

        </LinearLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

ButtonActivity.java

public class ButtonActivity extends MyBaseActivity {

    Button btn_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_com_button);

        btn_button = findViewById(R.id.btn_button);
        btn_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ButtonActivity.this, "You clicked button", Toast.LENGTH_LONG).show();
            }
        });
    }

}

setOnClickListener():通过此方法设置点击监听。
View.OnClickListener():实例化一个匿名类,重写其onClick()方法,在其中编写点击按钮后的业务逻辑。


上一篇 下一篇

猜你喜欢

热点阅读