【问题标题】:Custom onClickListner in custom widget - how?自定义小部件中的自定义 onClickListener - 如何?
【发布时间】:2014-09-03 12:13:46
【问题描述】:

我有一个扩展线性布局的自定义小部件。 它只是一小段文字。

这是小部件的 xml:

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:background="@drawable/actionbar_background"
        android:layout_height="35dp"
        android:paddingLeft="15dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/title"
            android:layout_height="35dp"
            android:textColor="@color/white"
            android:layout_width="match_parent"
            android:textSize="18dp"
            android:clickable="true"
            android:gravity="center_vertical" />
    </LinearLayout>

    <TextView
        android:id="@+id/ingreds"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Some text"
        android:paddingLeft="15dp" />
</LinearLayout>

如果我在 Activity 的某处为该自定义小部件创建 onClickListener - 只有当我单击该布局中的最后一个 TextView 时它才会做出反应。但我需要为包含“标题”textView 的水平LinearLayout 设置onClickListener,或者直接在标题TextView 上设置。 重要提示:我想将这些侦听器 OUTSIDE 设置为自定义小部件类。

我认为我需要在我的自定义类或类似的东西中重写 setOnclickListener 方法,但我不确定如何实现。

这是自定义小部件的类:

public class MyTextBlock extends LinearLayout {
    private TextView title;
    private TextView ingreds;

    public MyTextBlock(Context context) {
        this(context, null);
    }

    public MyTextBlock(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.my_text_block, this, true);

        title = (TextView) findViewById(R.id.title);
        ingreds = (TextView) findViewById(R.id.ingreds);
    }

    public void setText(String text) {
        ingreds.setText(text);
    }

    public void setTitle(String titleText) {
        title.setText(titleText);
    }
}

感谢您的回答。

【问题讨论】:

标签: java android widget listener


【解决方案1】:

只需为这个视图类实现 OnClickListener 并设置textview.setOnClickLister(this);

现在在onClick(View v) 方法下处理点击事件。

【讨论】:

  • 你能给我提供示例代码吗?我之前从未创建过自定义小部件,所以我没有完全理解你。谢谢。
  • 好的,我做到了。但这不是我想做的。我可以处理在视图类内部单击 textview,但我需要在外部处理它。例如:我在 MainActivity 中创建了新的 MyTextBlock 对象。所以我需要处理从 MainActivity 点击 textView(在自定义 View 类中)。
  • 好的,在初始化自定义视图并将其设置为 textview 时,不仅仅是从主活动类传递 onClickListener,现在对 textview 的点击将在 onClick(View v) 方法下捕获你的主要活动课程。
  • 感谢您的解决方案。那是工作。但我以其他方式做到了,为小部件创建了一个自定义侦听器。这是必要的,因为小部件的使用发生了一些变化。不过还是谢谢!
【解决方案2】:
MyTextBlock textviewCustom = ....
textviewCustom.setonclickListner(MainClass.this); 


public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;

public MyTextBlock(Context context) {
    this(context, null);
}


public MyTextBlock(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.my_text_block, this, true);

    title = (TextView) findViewById(R.id.title);
    ingreds = (TextView) findViewById(R.id.ingreds);
}


public void setonclickListner(OnclickListner listner)
{
 title.setonclicklistner(listner);
 ingreds.setonclicklistner(listner);
}

public void setText(String text) {
    ingreds.setText(text);
}

public void setTitle(String titleText) {
    title.setText(titleText);
}
}

【讨论】:

  • 这似乎行不通。 OnClick 仍然处理单击 ingreds TextView,而不是标题(在 SetOnclickListener 方法中,我删除了“ingreds.setonclicklistner(listner);”行)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多