【问题标题】:How can I print drawable images names [duplicate]如何打印可绘制图像名称[重复]
【发布时间】:2016-02-03 22:32:33
【问题描述】:

我有一个带有 1 个文本视图、1 个图像视图和 1 个按钮的布局。我有 5 张可绘制的图片。他们的名字像image1,image2,image3......当我点击按钮时,按钮改变drawable中的随机图片(“image”+rand.numbers方法)但我想将更改的图像名称打印到textview。我该怎么做? *简要地;按钮功能:首先打印现有图像>然后更改新的随机图像。 **我很抱歉语法不好..

public void onClick(View v) {
    ImageView img=(ImageView)findViewById(R.id.imageView1);
    Random rand = new Random();
    int rndInt = rand.nextInt(5) + 1;
    String drawableName = "image"+ rndInt;
    int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    img.setImageResource(resID);
};

【问题讨论】:

  • 不,不一样?我的问题是,如何将现有的随机图片名称打印到 textview 之后,我想在 1 个按钮中更改所有新的随机图片。
  • 艾伦,也许如果你展示你的代码,我们会更好地理解你想要什么
  • 我们可以与其他平台交谈吗?也许是 Skype 或 fb?
  • public void onClick(View v) { ImageView img=(ImageView)findViewById(R.id.imageView1);随机 rand = new Random(); int rndInt = rand.nextInt(5) + 1;字符串drawableName =“图像”+ rndInt; int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName()); img.setImageResource(resID); };此代码在我的按钮中。当我单击按钮系统更改我的可绘制文件夹中的随机图像时。我想在 textview 中打印图像名称,例如系统更改 image2,image2 现在在 imageview 中,我想将 textview 打印到 image2 我想我现在可以告诉

标签: android


【解决方案1】:

使用以下代码,您将始终看到当前显示的图像名称:

String  lastImageName = "";

public void onClick(View v) {
    ImageView img=(ImageView)findViewById(R.id.imageView1);
    Random rand = new Random();
    int rndInt = rand.nextInt(5) + 1;
    String drawableName = "image"+ rndInt;
    int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
    img.setImageResource(resID);

    // just set the text in the textview. You need to have it in your layout xml of course
    TextView textView = (TextView)findViewById(R.id.logoismi);
    textView.setText(lastImageName);

    lastImageName = drawableName;
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/bos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/dogru"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="BOŞ" />

    <ImageView
        android:id="@+id/logolar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bos"
        android:layout_marginBottom="70dp"
        android:maxHeight="800dp"
        android:maxWidth="800dp"      
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
        android:src="@drawable/image1" />

    <TextView
        android:id="@+id/logoismi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dogru"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:text="image1" />

</RelativeLayout>

【讨论】:

  • 感谢 Gavriel,但此代码首先更改图像,然后打印新图像名称。我需要第一个现有图像名称,然后更改新图像并循环...
  • 显示你的布局 xml
  • 其实我通过teamviewer向你展示了我的项目,我们应该现场讨论一下,我觉得会更好。
  • Gavriel,想象一下,我的按钮名称是“正确!”我们有一个随机图像,我应该将最后一个图像名称打印到 textview。我们不需要新的图像名称。我们需要在图像名称之前,因为它可能是真的,我们不能对新图像说任何话..也许它不会正确..
猜你喜欢
  • 2011-09-30
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-26
相关资源
最近更新 更多