i am thinking of a simple program which will retrieve the appropriate image file (there will be many image files within drawable) from drawables depending on the user input from an EditText view. this input will match with 1 of the the png file names in my drawable resource folder. The program will thus retrieve the png file with the name that has the same name as the text in edittext. following is my xml file:
<?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"
/>
and following is my .java file.
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);
setCont开发者_开发百科entView(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);
}
});
}
}
i followed Vladimir's suggestions..but its giving me a force close..also once i get the id of the image in drawable..i want to set that image on the UI..
Use reflection in order to do it:
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);
OK i got the solution using a hashmap following is my new java file:
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);
}
});
}
}
精彩评论