从Android可绘制资源名称中检索Java字符串

时间:2015-07-25 10:32:46

标签: java android

我有一系列可绘制的png文件,如下所示:

int[] images = {R.drawable.alabama, R.drawable.alaska, R.drawable.arizona};

currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);

我怎样才能得到这样的东西:

String imageName = "alabama"

我尝试了这个,但是它返回了我的图像视图ID而不是src名称。

String imageName = getResources().getResourceEntryName(R.id.idImageViewPic);

更新 这是我在上下文中获取内容的更多代码。我在下面修改了一个答案,但是我收到一个错误:不兼容的类型:字符串无法转换为int。

public class GameFragment extends Fragment {

    private AlertDialog mDialog;
    private ImageView hImageViewPic;
    private Button iButton, gButton;

    private int currentImage = 0;
    int[] images = {R.drawable.alabama, R.drawable.alaska, R.drawable.arizona};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_game, container, false);
        hImageViewPic = (ImageView) rootView.findViewById(R.id.idImageViewPic);
        iButton = (Button) rootView.findViewById(R.id.guess_button);

        final EditText responseField = (EditText) rootView.findViewById(R.id.ResponseText);



        // Handle buttons here...
        View guessButton = rootView.findViewById(R.id.guess_button);
        guessButton.setOnClickListener(new View.OnClickListener() {
            int score = 0;

            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(getActivity());
                String response = responseField.getText().toString();
                String wrongTextAnnouncement = getString(R.string.wrong_text, response);

                int resourceId = images[currentImage];
                String name = getResources().getResourceEntryName(resourceId);
                hImageViewPic.setImageResource(resourceId);
                hImageViewPic.setTag(name);
                String imageName = hImageViewPic.getTag(name);

                Log.d("UT3", response);
                Log.d("UT3", wrongTextAnnouncement);
                Log.d("UT3", imageName);
                if (imageName != response) {
                    builder.setTitle(R.string.wrong_title);
                    builder.setMessage(wrongTextAnnouncement);
                    builder.setCancelable(false);
                    builder.setPositiveButton(R.string.ok_label,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface,
                                                    int i) {
                                    // nothing
                                }
                            });
                    mDialog = builder.show();
                } else {
                    builder.setTitle(R.string.right_title);
                    builder.setMessage(R.string.right_text);
                    builder.setCancelable(false);
                    builder.setPositiveButton(R.string.ok_label,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface,
                                                    int i) {
                                    // nothing
                                }
                            });
                    score += 10;
                    String scoreString = String.valueOf(score);
                    Log.d("UT3", scoreString);
                }
                mDialog = builder.show();
                currentImage++;
                currentImage = currentImage % images.length;
                hImageViewPic.setImageResource(images[currentImage]);
            }
        });
        return rootView;
    }

2 个答案:

答案 0 :(得分:0)

您正在接收View的ID,因为您要将View的ID传递给

getResourceEntryName(int resid)

如果您希望String imageName = "alabama"传递给drawable的方法ID,而不是ImageView

但是没有办法检查为图像视图设置了什么资源ID,因为引擎盖 Drawable 是根据传递的ID创建的。

幸运的是,您可以使用hImageViewPic.setTag(Object object)稍后阅读。

所以代码可能如下:

int[] images = {R.drawable.alabama, R.drawable.alaska, R.drawable.arizona};

currentImage++;
currentImage = currentImage % images.length;
int resourceId = images[currentImage];
String name = getResources().getResourceEntryName(resourceId);
hImageViewPic.setImageResource(resourceId);
hImageViewPic.setTag(name);

稍后您可以使用:hImageViewPic.getTag()

获取图片名称

答案 1 :(得分:0)

在我的意见中,最好使用字符串图像数组,如:

String[] imagesTitle = {alabama, alaska,arizona};

然后获取图片ID:

String imageName = imagesTitle[currentImage];
int drawableResourceId = this.getResources().getIdentifier(imageName,"drawable", this.getPackageName())

“this”是一个活动,只是为了澄清。 然后你可以为图像设置标签并为你的imageview设置id,如:

hImageViewPic.setImageResource(drawableResourceId);
hImageViewPic.setTag(imageName);

我希望这可以提供帮助!