点击儿童Android

时间:2016-03-22 18:07:37

标签: android

如何正确点击当前名称为test的子textview。布局是以编程方式生成的。我需要获取LinearLayout的子元素的id,然后单击它。任何例子或解决方案?

  ll = new LinearLayout(context);
    ll.setId(R.id.testing123);
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    ll.setOrientation(LinearLayout.VERTICAL);
    lp2.setMargins(30, 30, 30, 30);
    ll.setLayoutParams(lp2);
    ll_root.addView(ll);

    TextView test = new TextView(context);
    LinearLayout.LayoutParams lp234 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    test.setText("TEST 123");
    test1.setId(R.id.a);
    test.setLayoutParams(lp234);
    test.setClickable(false);
    lp234.setMargins(20, 20, 30, 30);


    ll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.a:
                    Log.e("AAAA", "AAAA");
                    break;
            }
        }
    });
    ll.addView(test);

1 个答案:

答案 0 :(得分:0)

如果您正在询问,可以使用ll.findViewById(R.id.a)找到linearlayout的子项。如果没有,你能澄清一下吗?