我有以下自定义组件:
<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);
}
});
我做错了什么?
谢谢大家
答案 0 :(得分:1)
您忘了在onClick()方法的Toast行末尾添加“.show()”! :)
Toast.makeText(getApplicationContext(),“test”,Toast.LENGTH_SHORT).show();