通过使用名称动态地从drawables中检索图像资源

时间:2011-05-13 10:48:17

标签: android image resources

我正在考虑一个简单的程序,它将从drawables中检索适当的图像文件(drawable中将有许多图像文件),具体取决于EditText视图中的用户输入。此输入将与我的drawable资源文件夹中的1个png文件名匹配。因此,程序将检索具有与edittext中的文本同名的名称的png文件。以下是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="enter the resource id and press getit button to retrieve it"
    />
<EditText
    android:id="@+id/edtxt"
    android:layout_width="100px" 
    android:layout_height="100px"    
    />
<EditText
    android:id="@+id/stedtxt"
    android:layout_width="100px" 
    android:layout_height="100px"    
    />    

<TextView  
    android:id="@+id/txtved"
    android:layout_width="60px" 
    android:layout_height="50px" 

    />
<Button
    android:id="@+id/Button01"
    android:layout_width="70px" 
    android:layout_height="70px"    
    android:clickable="true"
/> 

以下是我的.java文件。

package com.example.retrievedrawable;

import java.lang.reflect.Field;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class RetrieveDrawable extends Activity {
    private Editable resid;
    private String residst;
    private String TAG;
    private int drawableId;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText edresid=  (EditText)findViewById(R.id.edtxt); 
        final EditText stdtxt=  (EditText)findViewById(R.id.stedtxt);
        Button setit = (Button) findViewById(R.id.Button01);
        final Editable resid=(Editable)edresid.getText();
        residst=resid.toString();


        try {
            Field field = com.example.retrievedrawable.R.drawable.class.getField(residst);
            try {
                drawableId= field.getInt(null);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (NoSuchFieldException e) {
            Log.e(TAG, "Can't find resource drawable with name " + residst);
        } 

        setit.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
              stdtxt.setText(drawableId);
            }

            }); 
    }       

}

我跟随弗拉基米尔的建议..但它给了我一个力量关闭..所以一旦我得到图像的id在drawable ..我想在UI上设置该图像..

2 个答案:

答案 0 :(得分:3)

使用反射来执行此操作:

        try {
            Field field = com.lid.lines.R.drawable.class.getField(imageResource);
            return field.getInt(null);
        } catch (NoSuchFieldException e) {
            Log.e(TAG, "Can't find resource drawable with name " + imageResource);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Can't access to resource drawable with name " + imageResource);
        }
        throw new IllegalStateException("Failed to get resource id of " + imageResource); 

答案 1 :(得分:0)

好的,我使用hashmap获得解决方案以下是我的新java文件:

    import android.app.Activity;
    import android.content.res.Resources;
    import android.content.res.TypedArray;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.text.Editable;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class RetrieveDrawable extends Activity {
        private Editable resid;
        private String residst;
        private String TAG;
        private int drawableId;
        private Object ImageView;
        public static final String PREFIX = "img";
        public static final Map mImageMap = new HashMap<String, Drawable>();

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            EditText edresid=  (EditText)findViewById(R.id.edtxt); 
            final EditText stdtxt=  (EditText)findViewById(R.id.stedtxt);
            Button setit = (Button) findViewById(R.id.Button01);
            final ImageView imview =(ImageView)findViewById(R.id.imageView);
            final Editable resid=(Editable)edresid.getText();
            residst=resid.toString();
            if (mImageMap.size() == 0) {
                   mImageMap.put(PREFIX + "a", getResources().getDrawable(R.drawable.fl1a1));
                   mImageMap.put(PREFIX + "2", getResources().getDrawable(R.drawable.lf1a1));
                   mImageMap.put(PREFIX + "3", getResources().getDrawable(R.drawable.st1a1));
                }


            setit.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
                  String exampleInput = resid.toString();
                  Drawable d = (Drawable) mImageMap.get(PREFIX + exampleInput); 
                  imview.setBackgroundDrawable(d);

              }


             });  


        }       

 }