2011-05-13 37 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

它不工作........這說明不了什麼,鑑於 –