.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普通按钮那一篇