我写了一个类,继承了linearLayout,然后把他当做一个控件放在activity_main.xml里,然后又在这个控件加了个textView,然后想在这个类里设置点击监听器对这个textView进行点击监听,可是这样报错,可视化编程那里都没能显示出来,这是为什么,如何修改呢
我已解决,我把设置监听器的方法放入其父类方法onFinishInflate()后就启动成功
详细代码贴上来才能分析
如果你写了tv_app_manager_title.setOnClickListener(this);, 或者 onclick 应该,使用其中之一,这个 textiew:
android:layout_width="fill_parent" 写了么?
另外:直接过去 textview 中的id 呗,
不过我感觉聪明的你应该问的是在 引入的资源文件中 如何过去。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(r.layout.xxx, null);
TextView tv = (TextView) view.findViewById(R.id.tv);
你确定textview是clickable的?试着自定义一个textview并设为可点击看看
1.布局文件
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#F0F0F0" >
<TextView
android:id="@+id/errorText"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="View监听测试"
android:textColor="#FFFFFF"
android:background="#0072E3"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<LinearLayout
android:id="@+id/show1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#FF0000" >
<Button
android:id="@+id/clickButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/show2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#00FF00" >
</LinearLayout>
<LinearLayout
android:id="@+id/show3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#0000FF" >
</LinearLayout>
2.Activity部分代码TestClick.java
找到所选控件或布局
[java] view plain copy
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.show1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.show2);
LinearLayout linearLayout3 = (LinearLayout) findViewById(R.id.show3);
Button button = (Button) findViewById(R.id.clickButton);
布局增加监听
[html] view plain copy
linearLayout.setOnTouchListener(new TouhListener());
linearLayout.setOnClickListener(new ClickListenr());
linearLayout2.setOnClickListener(new ClickListenr2());
linearLayout3.setOnClickListener(new ClickListenr3());
布局下按钮控件增加监听
[html] view plain copy
button.setOnClickListener(new ClickListenrB());
监听类的覆盖
[html] view plain copy
class TouhListener implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), "---- OnTouchListener -----", event.getAction()).show();
Intent intent = new Intent();
intent.setClass(TestClick.this, UdoActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out); // 实现淡入淡出
return true;
}
}
class ClickListenrB implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button被触发", Toast.LENGTH_LONG).show();
}
}
class ClickListenr implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "linearLayout被触发", Toast.LENGTH_LONG).show();
}
}
class ClickListenr2 implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "linearLayout2被触发", Toast.LENGTH_LONG).show();
}
}
class ClickListenr3 implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "linearLayout3被触发", Toast.LENGTH_LONG).show();
}
}
到此得到的结果是,任何布局或是控件都可以正确响应。但是当在布局中动态增加View显示时,监听就会出现上述所遇到的情况了:
[html] view plain copy
linearLayout.addView(timechartView, layoutParams);
这个时候如果不给新增加的View监听的话,所在的区域不会响应linearLayout的监听,如此需要给新增加的View加上监听。
[java] view plain copy
timechartView.setOnTouchListener(new TouhListener());
View view = View.inflate(context, R.layout.linearlayout,null);
textView = (TextView)view.findViewById(R.id.textView);
首先用上面的方法找到textView外层的LinearLayout布局,然后在Linearlayout布局下使用findviewbyid就可以设置点击事件了。