如何为自定义LinearLayout类设置setOnClickListener

时间:2015-03-09 00:04:48

标签: android class onclicklistener

我为自定义LinearLayout创建了此类:

public class TestViewButton extends LinearLayout {

    TextView text;
    View line;
    private String sText;
    private boolean showLine;

    public TestViewButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public TestViewButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.it_button, this, true);
        text = (TextView) findViewById(R.id.text);
        line = (View) findViewById(R.id.line);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.testViewButton);
        sText = a.getString(R.styleable.testViewButton_buttonText);
        showLine = a.getBoolean(R.styleable.testViewButton_showLine, true);
        a.recycle();
        text.setText(sText);
        if(showLine){
            line.setVisibility(View.VISIBLE);
        }
    }

    public TestViewButton(Context context) {
        super(context);
    }
}

从我的Activity开始,我为它设置了setOnClickListener

TestViewButton testViewButton b1 = (TestViewButton) findViewById(R.id.btn1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent(ActivityA.this, ActivityB.class));
        }
    });

但是它不起作用而且没有任何反应 - 当我认为应该回调时,不会调用回调。该类执行回调操作的大部分工作;但遗憾的是,setOnClickListener不会调用我实施的这些行动。

2 个答案:

答案 0 :(得分:0)

TestViewButton testViewButton b1 = (TestViewButton) findViewById(R.id.btn1);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent(ActivityA.this, ActivityB.class));
        }
    });

使用上面的代码,解决问题

答案 1 :(得分:0)

我解决了问题:) 在xml文件中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.balysv.materialripple.MaterialRippleLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_weight="1"
    app:rippleColor="#50FFFFFF" >

    <LinearLayout
        android:id="@+id/clickBase"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal" >

        <com.lib.fontly.TextView
            android:id="@+id/text"
            style="@style/font_traffic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|right"
            android:text="text"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />
    </LinearLayout>
</com.balysv.materialripple.MaterialRippleLayout>

<View
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="#50FFFFFF"
    android:visibility="gone"
    android:orientation="vertical" >
</View>

如你所见,我正在使用MaterialRippleLayout库,我的问题从这里开始,所以我把课程改为:

public class TestViewButton extends LinearLayout {

TextView text;
View line;
private String sText;
private boolean showLine;
LinearLayout clickBase;

public TestViewButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public TestViewButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.it_button, this, true);
    text = (TextView) findViewById(R.id.text);
    line = (View) findViewById(R.id.line);
    clickBase= (LinearLayout) findViewById(R.id.clickBase);
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.testViewButton);
    sText = a.getString(R.styleable.testViewButton_buttonText);
    showLine = a.getBoolean(R.styleable.testViewButton_showLine, true);
    a.recycle();
    text.setText(sText);
    if(showLine){
        line.setVisibility(View.VISIBLE);
    }
}

public TestViewButton(Context context) {
    super(context);
}

public void setOnClickListener(OnClickListener cl){
    clickBase.setOnClickListener(cl);
}

现在它的工作正常。 所有坦克。