Android 如果用户单击TextView,如何启用跳转到链接页面?
XML布局Android 如果用户单击TextView,如何启用跳转到链接页面?,android,Android,XML布局 <LinearLayout style="@style/behindMenuScrollContent" android:paddingTop="25dp" > <TextView style="@style/behindMenuItemTitle" android:text="People" /> <LinearLayout android:id="@+id/iconViewL
<LinearLayout style="@style/behindMenuScrollContent"
android:paddingTop="25dp" >
<TextView
style="@style/behindMenuItemTitle"
android:text="People" />
<LinearLayout
android:id="@+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<jp.fureco.IconView
android:id="@+id/iconViewItem4"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"/>
<TextView
android:layout_marginLeft="10dp"
style="@style/behindMenuItemLabel"
android:text="Visitor" />
</LinearLayout>
</LinearLayout>
当用户单击图标查看链接4
时,我希望它跳转到url“”
如何编写代码?使其可单击,并为其提供OnClickListener(您可以在任何视图上执行此操作)
findviewbyd(R.id.iconViewLink4).setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//在这里做点什么
}
});
是的,你可以。只需在
中使用android:onClick=“methodName”,或者使用类似setOnClickListener的程序添加它
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}
如果你有像4-iconViewLink20这样的iconViewLink4,你会怎么做?你会写17次代码吗?如果你想要一个链接列表,使用ListView并让每个项目存储它想要打开的链接。添加OnListItemClickListener,从单击的项目获取链接,然后执行所需操作。出于布局目的,我无法使用ListView。那么你会把你的代码放17次吗?“为了布局的目的,我不能使用ListView”-你打算在屏幕上放17次这样的东西吗?如果是这样,请考虑使用某种基于适配器的视图,即使它不是ListView。@MKK:IDE的Eclipse有许多有用的工具来帮助您修复简单的语法错误。如果你理解这个方法,你可以自己编写代码,而不是从这里复制代码。如果你有像4-ICONVIEWINK20这样的ICONVIEWINK4,你会怎么做?你会写17次代码吗?
<LinearLayout
android:id="@+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
findViewById(R.id.iconViewLink4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something here
}
});
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}