【问题标题】:how to setOnClickListener for custom LinearLayout class如何为自定义 LinearLayout 类设置 OnClickListener
【发布时间】:2015-03-09 00:04:47
【问题描述】:

我为我的自定义 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 不会调用我实施的这些操作。

【问题讨论】:

  • 显示布局xml文件,它可能提供线索

标签: android class onclicklistener


【解决方案1】:
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));
        }
    });

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

【讨论】:

    【解决方案2】:

    我解决了问题
    在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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多