2012-02-04 25 views
0

我有以下的自定义组件:自定义组件不能引发听众关闭

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="@dimen/bottom_actionbar_item_height" 
android:orientation="horizontal" 
android:gravity="center"> 

    <Button 
     android:id="@+id/btnLogin" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/botton_button_selector" 
     android:drawableLeft="@drawable/balloon_overlay_close"    
     android:text="@string/bottonbar_earned" 
     android:layout_weight="1" android:clickable="true" android:focusable="true" 
     android:layout_marginRight="10dp"/> 

    <Button 
     android:id="@+id/btnLogin2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/botton_button_selector" 
     android:drawableLeft="@drawable/balloon_overlay_close" 
     android:layout_marginRight="10dp" 
     android:text="@string/bottonbar_inprogress" android:focusable="true" android:clickable="true" />   

    <Button 
     android:id="@+id/btnLogin3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/botton_button_selector" 
     android:drawableLeft="@drawable/balloon_overlay_close" 
     android:text="@string/bottonbar_redeemed" 
     android:layout_marginRight="10dp" 
     android:layout_weight="1" android:focusable="true" android:clickable="true"/> 

</LinearLayout> 

我有我的main.xml中其中有一个主要的RelativeLayout,上面有许多其他布局,其中之一是我的自定义组件。自定义组件显示正确,但没有侦听器被调用。 我的自定义组件类具有下面的代码:

public class BottomActionBar extends LinearLayout implements OnClickListener{ 

private LayoutInflater mInflater; 
private LinearLayout mBarView; 

public BottomActionBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mBarView = (LinearLayout) mInflater.inflate(R.layout.bottom_actionbar, null); 
    addView(mBarView); 

    Button bt1 = (Button) mBarView.findViewById(R.id.btnLogin); 
    bt1.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Toast.makeText(getContext(), "test", Toast.LENGTH_SHORT); 
     } 
    }); 
} 

@Override 
public boolean onTouchEvent(MotionEvent evt) { 
    System.out.print("test"); 
    return super.onTouchEvent(evt); 
} 

public void onClick(View v) { 
    System.out.print("test");  
} 

}

现在,这个测试的时候,什么也不显示!我也尝试在主布局中给组件充气:

mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mBarView = (LinearLayout) mInflater.inflate(R.layout.bottom_actionbar, null); 

    Button bt1 = (Button) mBarView.findViewById(R.id.btnLogin); 
    bt1.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT);     
     } 
    }); 

我在做什么错?

谢谢大家

回答

1

你忘了加上“.show()”在的onClick()方法的吐司行的结尾! :)

Toast.makeText(getApplicationContext(),“test”,Toast.LENGTH_SHORT).show();

+0

嗯,现在这是非常尴尬:O 在过去的一小时卡住了...谢谢! – 2012-02-04 19:04:30