package cn.edu.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
public class ButtonActivity extends Activity {
/** Called when the activity is first created. */
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)this.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("btn pressed");
}
});
}
}
在res文件夹下,新建drawable文件夹,同时,新建ic_tab_artists.xml文件
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/android_focused"
android:state_focused="true" />
<item android:drawable="@drawable/android_normal" />
</selector>
在布局文件中
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_tab_artists"
/>
本文介绍了一个简单的Android应用程序示例,展示了如何为按钮设置点击监听器,并通过XML定义按钮的不同状态样式。此外,还提供了创建按钮及设置监听器的具体代码实现。
632

被折叠的 条评论
为什么被折叠?



