2011-05-13 67 views
0

我想到一个简单的程序,将获取适当的图像文件的检索可绘制的图像资源(会有绘制中的许多图像文件)从依赖于从一个EditText视图用户输入可绘制。此输入将与我的drawable资源文件夹中的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); 
      } 

      }); 
    }  

} 

我也跟着弗拉基米尔suggestions..but它给了我力量close..also一次我在drawable..i获得图像的id要设置的用户界面,图像..

+0

如果摹et强制关闭,总是附加logcat的日志。 –

回答

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); 
+0

感谢弗拉基米尔...但我M于执行此代码,其中包括乌拉圭回合的建议得到力闭合......看到上面的编辑代码.. –

0

OK我有使用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); 

       } 


      }); 


     }  

} 
+0

它不工作........这说明不了什么,鉴于 –