image.png
layout.xml:
<?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"
tools:context="com.example.my_imagebutton.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="下载战狼点这里,你懂的"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_button"
android:onClick="onImageButtonClick"
android:src="@drawable/image_"
/>
<!--onImageButtonClick与java代码中类对应-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Hello World!"-->
<!--app:layout_constraintBottom_toBottomOf="parent"-->
<!--app:layout_constraintLeft_toLeftOf="parent"-->
<!--app:layout_constraintRight_toRightOf="parent"-->
<!--app:layout_constraintTop_toTopOf="parent" />-->
</LinearLayout>
package com.example.my_imagebutton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button1);
ImageButton imageButton = findViewById(R.id.image_button);
}
//4.在XML中添加onClick属性
public void onButtonClick(View view){
new AlertDialog.Builder(MainActivity.this).
setTitle("对话框").setIcon(R.mipmap.ic_launcher).
setMessage(R.string.d_string).show();
}
public void onImageButtonClick(View view){
new AlertDialog.Builder( MainActivity.this).
setTitle("对话框").setIcon(R.mipmap.ic_launcher).
setMessage("哈哈,被骗了吧").show();}
//自定义一类
public class MyOnClick implements View.OnClickListener{
@Override
public void onClick(View view) {
}
}
public class MyImageOnClick implements View.OnClickListener{
@Override
public void onClick(View view) {
}
}
}
string.xml
<resources>
<string name="app_name">My_imagebutton</string>
<string name="d_string">你觉得我会让你下载吗</string>
<string name="b_string">想下载战狼2吗,点我呀!!!</string>
</resources>
网友评论