.xml文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageButton
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple"/>
    <ImageButton
        android:id="@+id/zhuce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/banana"
        android:onClick="myClick"/>
</LinearLayout>

,java文件代码如下:

package com.example.button;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton b=(ImageButton) findViewById(R.id.login);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast=Toast.makeText(MainActivity.this,"你单击了“苹果”按钮",Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }
    public void myClick(View view){
        Toast toast=Toast.makeText(MainActivity.this,"你单击了“香蕉”按钮",Toast.LENGTH_SHORT);
        toast.show();
    }
}

Android studio 图片按钮
在此不做过多解释,与普通按钮类似,可看Android studio普通按钮那一篇

相关文章:

  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-11-28
猜你喜欢
  • 2021-06-11
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-07-02
  • 2022-01-08
  • 2021-08-16
相关资源
相似解决方案