把需要的图片按钮放到项目res文件夹下的drawable-hdpi文件夹里。添加ImageButton按钮
.xml文件代码如下
package com.example.android_button; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.Toast; public class ImageButtons extends Activity implements OnClickListener{ private ImageButton imagebtn1,imagebtn2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.imagebuttons);//每次创建完XML,再写对应的类的时候,这句话不要忘记,否则会闪退 imagebtn1=(ImageButton) this.findViewById(R.id.imageButton1); imagebtn2=(ImageButton) this.findViewById(R.id.imageButton2); imagebtn1.setOnClickListener(this); imagebtn2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.imageButton1: Toast.makeText(ImageButtons.this, "i am a clock", 1).show(); break; case R.id.imageButton2: Toast.makeText(ImageButtons.this, "i am a share", 1).show(); break; } } }//运行效果图如下
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219437.html原文链接:https://javaforall.net
