Android之Button练习

常用功能

Android之Button练习_第1张图片

先声明创建一个Button

activity_main.xml


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

    <Button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"/>

    <Button
        android:id="@+id/btn_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"/>


LinearLayout>

MainActivity.java

package com.example.helloword;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    //声明空间
    private Button mBtnTextView;
    private Button mBtnButton;

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

        mBtnTextView = findViewById(R.id.btn_textview);//找到空间
        mBtnTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到TextView演示界面
                Intent intent = new Intent(MainActivity.this,TextViewActivity.class);
                startActivity(intent);
            }
        });

        mBtnButton = findViewById(R.id.btn_button);
        mBtnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到Button演示界面
                Intent intent = new Intent(MainActivity.this,ButtonActivity.class);
                startActivity(intent);
            }
        });
    }
}

再新建演示Button的Activity

activity_button.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="20sp"
        android:textColor="#0066FF"
        android:background="#CCFFFF"/>

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_below="@id/btn_1"
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:textColor="#0066FF"
        android:background="@drawable/bg_btn2"/>





    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        android:textColor="#0066FF"
        android:background="@drawable/bg_btn3"
        android:layout_below="@id/btn_2"
        android:layout_marginTop="20dp"/>


    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:textSize="20sp"
        android:textColor="#0066FF"
        android:background="@drawable/bg_btn4"
        android:layout_below="@id/btn_3"
        android:layout_marginTop="20dp"
        android:onClick="showToast"/>







    <Button
        android:id="@+id/btn_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:textSize="20sp"
        android:textColor="#0066FF"
        android:background="#CCFFFF"
        android:layout_below="@id/btn_4"
        android:layout_marginTop="20dp"/>



RelativeLayout>

ButtonActivity.java

package com.example.helloword;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ButtonActivity extends AppCompatActivity {

    private Button mBtn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        mBtn5 = (Button)findViewById(R.id.btn_5);
        mBtn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ButtonActivity.this,"按钮5被点击了",Toast.LENGTH_SHORT).show();
            }
        });
    }

//    写showToast这个方法
    public void showToast(View view){
        Toast.makeText(this,"按钮4被点击了",Toast.LENGTH_SHORT).show();
    }
}

bg_btn2.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#FF99"/>

    <corners
        android:radius="15dp"/>


shape>

bg_btn3.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="#FF0000"/>

    <corners
        android:radius="15dp"/>


shape>

bg_btn4.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">

        <shape>
            <solid android:color="#FFFFFF"/>
            <corners android:radius="15dp"/>
        shape>
    item>

    <item android:state_pressed="false">
        
        <shape>
            <solid android:color="#CC7A00"/>
            <corners android:radius="15dp"/>
        shape>
    item>

selector>

Android之Button练习_第2张图片
图1
Android之Button练习_第3张图片
图2
Android之Button练习_第4张图片
图3

最终效果:

Button练习 Android Emulator - Nexus

你可能感兴趣的:(Android)