<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd">
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"
"http://www.wapforum.org/DTD/wml12.dtd">
我们打开http://www.wapforum.org/DT目录,可以看到里面的链接。根据实际需要把dtd复制过来就可以了。
import android.app.Activity; import android.os.Bundle; import android.text.InputFilter; import android.text.Spanned; import android.view.View; import android.widget.Button; import android.widget.EditText; /** * Class which shows how to lock and unlock EditText component * * @author FaYnaSoft Labs */ public class Main extends Activity { private EditText editText; private boolean value = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.textId); editText.setText("EditText component"); Button b = (Button) findViewById(R.id.btnId); b.setText("Lock/Unlock"); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (value) { value = false; } else { value = true; } lockUnlock(value); } }); } /** * Method which locks and unlocks editText component * @param value our boolean value which using in or if operator */ private void lockUnlock(boolean value) { if (value) { editText.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { return source.length() < 1 ? dest.subSequence(dstart, dend) : ""; } } }); } else { editText.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { return null; } } }); } } }
我们完全可以通过反射机制来修改一个对象的私有变量的值,由于该文章并不是讨论反射的的文章,所以这里只给出通过反射来修改私有变量值的代码,但并不作详细的说明:
我们创建一个新的类,叫BeanUtils.java
类得内容看其来如下:
public class BeanUtils {
private BeanUtils() {
}
/**
* 直接设置对象属性值,无视private/protected修饰符,不经过setter函数.
*/
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
makeAccessible(field);
try {
field.set(object, value);
} catch (IllegalAccessException e) {
Log.e("zbkc", "", e);
}
}
/**
* 循环向上转型,获取对象的DeclaredField.
*/
protected static Field getDeclaredField(final Object object, final String fieldName) {
return getDeclaredField(object.getClass(), fieldName);
}
/**
* 循环向上转型,获取类的DeclaredField.
*/
@SuppressWarnings("unchecked")
protected static Field getDeclaredField(final Class clazz, final String fieldName) {
for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
// Field不在当前类定义,继续向上转型
}
}
return null;
}
/**
* 强制转换fileld可访问.
*/
protected static void makeAccessible(Field field) {
if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
field.setAccessible(true);
}
}
}
该工具提供一个共有的方法:public static void setFieldValue(final Object object, final String fieldName, final Object value)来修改一个对象的私有变量的值。