单击文本以打开图像

时间:2013-12-04 07:31:59

标签: android android-fragments textview

我对此很新。

如何点击文字并让它打开保存在应用中的图片?

这是我到目前为止所做的,但我遇到了错误,我正在使用Fragments而且我不知道我是否采用了正确的方式。

谢谢!

这就是我现在在Fragment_6.java中所拥有的:

package com.rufflez.swipeytabs;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class Fragment_6 extends SherlockFragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_6, container, false);


TextView tv = (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String url = "http://androidcookbook.com/seam/resource/graphicImage/escapedn.png";             
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
}); 
}

}
在fragment_6.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

 <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="left"
    android:clickable="true"
    android:text="@string/procedures1"
    />

1 个答案:

答案 0 :(得分:1)

更改为

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v =inflater.inflate(R.layout.fragment_6, container, false);
// inflate the layout

TextView tv = (TextView) v.findViewById(R.id.textView1);
// initialize textview using inflated view object 
tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String url = "http://androidcookbook.com/seam/resource/graphicImage/escapedn.png";             
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
}); 
reuturn v; // return view
}