2012-02-04 46 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