Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
Button和ImageButton控件实例:
activity_main.xml源代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="我是Button"
/>
<ImageButton
android:id="@+id/imgbtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/myimage"
/>
</LinearLayout>
在MainActivity.java里给它实例化并加上按钮点击的监听事件:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
private Button btn;
private ImageButton imgbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化EditText
btn = (Button)findViewById(R.id.btn);
imgbtn = (ImageButton)findViewById(R.id.imgbtn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.v("------------", "我是Button");
}
});
imgbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.v("------------", "我是ImageButton");
}
});
}
}
运行结果:
点击第一个按钮:
Logcat窗口显示
点击第二个按钮:
Logcat窗口显示
Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305
步步为营_Android开发课[17]_用户界面之Button(按钮)
原文地址:http://blog.csdn.net/y18334702058/article/details/44923057