我在屏幕的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" android:background="@drawable/bg">
<Button android:text="Lets Get Started"
android:background="@drawable/btnbg"
android:id="@+id/btn_nav_let_strtd"android:layout_width="wrap_content"
android:layout_height="35px"
android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"
android:drawableRight="@drawable/arrow"
android:drawableLeft="@drawable/nav_getstart">
</Button>
设置点击听众
公共类NavigationScreen扩展了Activity实现OnClickListener { 私人按钮btn_nav_lets_get_strtd;
private LinearLayout ll_get_started;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation);
btn_nav_lets_get_strtd = (Button) findViewById(R.id.btn_nav_let_strtd);
btn_nav_lets_get_strtd.setClickable(true);
}
private void setOnclickListeners() {
btn_nav_lets_get_strtd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("clicked");
Toast.makeText(NavigationScreen.this, "fgd",Toast.LENGTH_SHORT).show();
}
});
}
}
但是当我尝试单击此按钮时,它不可点击而不可聚焦..如何将其设置为可点击???
并且drawable不是位图,它是一个statlist drawable btnbg.xmlis这里
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="true" >
<item
android:drawable="@drawable/nav_blue_bar"
android:state_pressed="true"
/>
任何帮助将不胜感激......
答案 0 :(得分:1)
在您的方法setOnClickListeners()
中查看您的代码,您没有调用方法onCreate()
! ,
要解决您的问题,您应该像这样调用您的方法:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation);
btn_nav_lets_get_strtd = (Button) findViewById(R.id.btn_nav_let_strtd);
btn_nav_lets_get_strtd.setClickable(true);//here you don't need to call this method because the button is clickable by default
//call your method setOnClickListeners() here
setOnClickListeners();
}
private void setOnclickListeners() {
btn_nav_lets_get_strtd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("clicked");
Toast.makeText(NavigationScreen.this, "fgd",Toast.LENGTH_SHORT).show();
}
});