[英]How do I open an image by clicking on a text
i am making my very first app and i need some help please. 我正在制作我的第一个应用程序,请给我一些帮助。
How do i open an image which is already preset in my app by clicking on a hyperlinked text? 如何通过单击超链接文本来打开应用程序中已预设的图像?
I am trying to 我在尝试着
For example, 例如,
"Refer to Image001" “请参阅Image001”
when the user taps on the word "Image001", a window opens up the preset picture and it closes when i press the back button. 当用户点击“ Image001”一词时,一个窗口打开预设图片,当我按下“后退”按钮时,它关闭。
This is what i have so far 这就是我到目前为止
In strings.xml 在strings.xml中
<string name="refer">Refer to Image001</string>
In activity_main.xml 在activity_main.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" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="@string/refer" />
</ScrollView>
</LinearLayout>
thanks 谢谢
You will have to have 2 TextViews. 您将必须具有2个TextViews。 The first one will have the text: "Refer to " and the second one will have the text "Image001". 第一个将带有文本:“ Refer to”,第二个将带有文本“ Image001”。 Then you set a click listener on the second textview with code like this: 然后,使用以下代码在第二个textview上设置一个点击侦听器:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
you must first set Id to your textView in xml file like below: 您必须首先将id设置为xml文件中的textView,如下所示:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="@string/refer" />
then set setOnClickListener
for your textview like following code: 然后为您的textview设置setOnClickListener
,例如以下代码:
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
}
});
after doing that in onClick
method you can call you image like following code 在onClick
方法中完成此操作后,您可以像以下代码一样调用您的图片
String url = "http://www.xxx.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
The best way to do this is to use DialogFragments . 最好的方法是使用DialogFragments 。 For example, this class extends DialogFragment and displays a picture inside ImageView. 例如,此类扩展DialogFragment并在ImageView中显示图片。
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment newInstance (Bitmap targetPicture) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putExtra("picture", targetPicture);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialogfrag, container, false);
Bitmap b = (Bitmap) getArguments().getExtra("picture");
ImageView iv = (ImageView) v.findViewById(R.id.targetImageView);
iv.setImageBitmap(b);
return v;
}
However, you need to define dialogfrag.xml inside your layout folder, like this: 但是,您需要在布局文件夹中定义dialogfrag.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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/targetImageView" />
</LinearLayout>
And finally inside your activity: 最后,在您的活动中:
OnClickListener myClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment d = MyDialogFragment.newInstance(targetPicture) //replace targetPicture with the picture you want to display
d.show();
}
};
TextView tv = (TextView) findViewById(R.id.txt1);
tv.setOnClickListener(myClickListener);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.