我花了很多时间尝试用clickable item启动新活动,用URL link打开浏览器页面来创建ListView
。
应为以下项目:
某些文本[URL在这里]
通过单击Some text
开始新活动
通过单击[URL] Web浏览器正在启动
[我试图像在here或here中一样实现它,但无论如何对我来说都是无效的。
也许我没有设置一些此处未提及的属性。
简单ListView:
<ListView
android:id="@+id/news_list_view_full"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"
android:padding="@dimen/content_padding_small"
android:layout_below="@id/news_header"/>
ListView的项目:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/news_item_padding"
android:orientation="vertical" >
<TextView
android:id="@+id/news_text_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorLink="@color/sosbg_orange"
android:textAppearance="@style/CustomRegularTextStyle"
android:textColor="@color/sosbg_white" />
</LinearLayout>
和适配器:
public View getView(...View view...) {
...
TextView text = (TextView) view.findViewById(R.id.news_text_view_text);
text.setText(Html.fromHtml("Hello world <a href='http://google.com'>http://google.com</a>"));
text.setMovementMethod(LinkMovementMethod.getInstance());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start new activity
}
}
}
因此,当前链接是可单击的,并打开具有相应页面的浏览器
但是listview的项目不可单击。结果,新活动无法开始。
您能否建议我可以尝试达到所需结果的一些步骤?
为什么要使用可点击的视图??
如果需要文本来单击,请使用TextView的文本对象而不是视图。
替换为。
view.setOnClickListener(new View.OnClickListener() {
}
像这样..
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start new activity
}
}
尝试使用textview的自动链接属性-
android:autoLink="web"
在您的示例中-
<TextView
android:id="@+id/news_text_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorLink="@color/sosbg_orange"
android:textAppearance="@style/CustomRegularTextStyle"
android:clickable="true"
android:autoLink="web"
android:textColor="@color/sosbg_white" />
您将代码添加为view.setOnClickListener(..)
,这里是view
,我将其视为text.setOnClickListener(..)
,如果正确,则将以下行添加到TextView
中
android:clickable="true"
并且您的布局应该是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/news_item_padding"
android:orientation="vertical" >
<TextView
android:id="@+id/news_text_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorLink="@color/sosbg_orange"
android:textAppearance="@style/CustomRegularTextStyle"
android:clickable="true"
android:textColor="@color/sosbg_white" />
</LinearLayout>
我希望这会对您有所帮助。
text.setClickable(true);
text.setFocusable(false);