将侦听器设置为自定义视图
Setting listener to custom view
我有一个 view
,它动态添加一个 custom view
,一个 checkbox
,一个 edittext
和一个 imagebutton
。我想将侦听器添加到 imagebutton
以删除 custom view
(或者如果按下 imagebutton
则将可见性设置为 gone
。这是我目前的代码,从更基本的东西:
public void addView() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.item_task_list, null);
ImageButton img_btn = findViewById(R.id.delete);
EditText name_et = findViewById(R.id.todo);
CheckBox done_cb = findViewById(R.id.done);
img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
parentLayout.addView(view, parentLayout.getChildCount() - 1);
只要我没有监听器,它就可以工作。有了它,我得到了这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
如果我像这样设置侦听器,它会起作用:
img_btn.setOnClickListener(this);
然后使用 activity 中的 OnClick
方法。我有:
case R.id.delete:
v.getParent().
break;
但是我无法将可见性设置为 gone
也无法将其删除。
这是我第一次添加 custom views
,所以我不确定我错过了什么,如果我应该做一个 class,从其他方式设置侦听器,或者如何访问它。
尝试从膨胀视图调用 findViewById
。应该有帮助。
public void addView() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.item_task_list, null);
ImageButton img_btn = view.findViewById(R.id.delete);
EditText name_et = view.findViewById(R.id.todo);
CheckBox done_cb = view.findViewById(R.id.done);
img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
parentLayout.addView(view, parentLayout.getChildCount() - 1);
我有一个 view
,它动态添加一个 custom view
,一个 checkbox
,一个 edittext
和一个 imagebutton
。我想将侦听器添加到 imagebutton
以删除 custom view
(或者如果按下 imagebutton
则将可见性设置为 gone
。这是我目前的代码,从更基本的东西:
public void addView() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.item_task_list, null);
ImageButton img_btn = findViewById(R.id.delete);
EditText name_et = findViewById(R.id.todo);
CheckBox done_cb = findViewById(R.id.done);
img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
parentLayout.addView(view, parentLayout.getChildCount() - 1);
只要我没有监听器,它就可以工作。有了它,我得到了这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
如果我像这样设置侦听器,它会起作用:
img_btn.setOnClickListener(this);
然后使用 activity 中的 OnClick
方法。我有:
case R.id.delete:
v.getParent().
break;
但是我无法将可见性设置为 gone
也无法将其删除。
这是我第一次添加 custom views
,所以我不确定我错过了什么,如果我应该做一个 class,从其他方式设置侦听器,或者如何访问它。
尝试从膨胀视图调用 findViewById
。应该有帮助。
public void addView() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.item_task_list, null);
ImageButton img_btn = view.findViewById(R.id.delete);
EditText name_et = view.findViewById(R.id.todo);
CheckBox done_cb = view.findViewById(R.id.done);
img_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
parentLayout.addView(view, parentLayout.getChildCount() - 1);