Пользовательский компонент не может отключить слушателей - PullRequest
0 голосов
/ 04 февраля 2012

У меня есть следующий пользовательский компонент:

<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 со многими другими макетами, и одна из них - мой пользовательский компонент.Пользовательский компонент отображается правильно, но слушатель не вызывается.Класс My Custom Component имеет следующий код:

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 Ответ

1 голос
/ 04 февраля 2012

Вы забыли добавить ".show ()" в конце строки Toast в методе onClick ()!:)

Toast.makeText (getApplicationContext (), "test", Toast.LENGTH_SHORT) .show ();

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...