2.28

安卓组件imagebutton


package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class ImageButton extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_button);
    }
}
package com.example.chapter02;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter02.utils.DateUtil;

public class ButtonOnClick extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_on_click);
        Button button=findViewById(R.id.button01);
        TextView tv05=findViewById(R.id.tv05);
        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                tv05.setText("你好,eee"+ DateUtil.getNowTime()+button.getText());
                tv05.setBackgroundColor(Color.RED);
                return true;
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv05.setText("你好,王凌霄"+ DateUtil.getNowTime()+button.getText());
                tv05.setBackgroundColor(Color.RED);
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <Button
        android:id="@+id/button01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击按钮"
        android:textColor="@color/black"
        android:background="#00ffff"
        />

    <TextView
        android:id="@+id/tv05"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:layout_gravity="center"
        />

</LinearLayout>
posted @ 2024-02-28 18:20  aallofitisst  阅读(9)  评论(0)    收藏  举报