有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从自定义视图访问片段中的onclicklistener

我有一组单选按钮,排列成3x3的网格。当我点击任何单选按钮时,都会出现一个祝酒词,但是在自定义视图中调用了onclick方法,我希望在片段中调用onclick方法

我的自定义视图

public class ToggleButtonGroupTableLayout  extends TableLayout  implements OnClickListener {
OnItemClickCallback onItemClickCallback;
private static final String TAG = "ToggleButtonGroupTableLayout";
private RadioButton activeRadioButton;

/**
 * @param context
 */
public ToggleButtonGroupTableLayout(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public ToggleButtonGroupTableLayout(Context context,OnItemClickCallback onItemClickCallback) {
    super(context);
    this.onItemClickCallback = onItemClickCallback;
    // TODO Auto-generated constructor stub
}

/**
 * @param context
 * @param attrs
 */
public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public void onClick(View v) {

    final RadioButton rb = (RadioButton) v;
    if ( activeRadioButton != null ) {
        activeRadioButton.setChecked(false);
    }
    rb.setChecked(true);
    Toast.makeText(getContext(),rb.getText().toString(), Toast.LENGTH_SHORT).show();

    activeRadioButton = rb;
}


public void radio_button_data(String s) {


}

/* (non-Javadoc)
 * @see 安卓.widget.TableLayout#addView(安卓.view.View, int, 安卓.view.ViewGroup.LayoutParams)
 */
@Override
public void addView(View child, int index,
                    安卓.view.ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    setChildrenOnClickListener((TableRow)child);
}


/* (non-Javadoc)
 * @see 安卓.widget.TableLayout#addView(安卓.view.View, 安卓.view.ViewGroup.LayoutParams)
 */
@Override
public void addView(View child, 安卓.view.ViewGroup.LayoutParams params) {
    super.addView(child, params);
    setChildrenOnClickListener((TableRow)child);
}


private void setChildrenOnClickListener(TableRow tr) {
    final int c = tr.getChildCount();
    for (int i=0; i < c; i++) {
        final View v = tr.getChildAt(i);
        if ( v instanceof RadioButton ) {
            v.setOnClickListener(this);
        }
    }
}

public int getCheckedRadioButtonId() {
    if ( activeRadioButton != null ) {
        return activeRadioButton.getId();
    }

    return -1;
}

我的碎片

public class Post_Property_1 extends Fragment{

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ToggleButtonGroupTableLayout.OnItemClickCallback callback = null;
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.post_property_1, container, false);}

请帮助我如何在片段活动中从自定义视图访问onclick。提前谢谢


共 (0) 个答案