android控件获得,android获取控件的几种方法

本文介绍了在Android中获取控件的四种方法:findViewById、getResources().getIdentifier、getField以及通过反射机制。详细解释了每种方法的使用步骤,并提供了具体的代码示例,帮助开发者更灵活地操作UI元素。

layout

android:layout_width="match_parent"

android:background="@color/colorPrimary"

android:layout_height="wrap_content">

android:id="@+id/item_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:textSize="25sp" />

0x01 findViewById

private TextView item_tv;

item_tv = findViewById(R.id.item_tv);

item_tv.setText("text1");

0x02 getResources().getIdentifier()

int viewId = getResources().getIdentifier("item_tv", "id", getPackageName());

TextView textView =findViewById(viewId);

textView.setText("text2");

0x03 getField()

try {

Field field = R.id.class.getField("item_tv");

TextView textView = findViewById(field.getInt(null));

textView.setText("text3");

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

0x04 通过反射

java反射机制

获取类的方式:(1)class.forName("类名"),(2)类名().getClass(),(3)类名.class;

Constructor类:

getConstuctor()返回class的构造函数;

getConstructors()返回class类的所有构造方法;

getDelaredConstructor()返回class类或接口指定的构造函数;

getDelaredConstructors()返回所有class类或接口指定的构造函数;

getEnclosingConstructor()返回class类的构造方法中的一个本地或匿名类;

getModifiers()返回对象的java修饰符;

getName()以字符串的形式返回构造方法的名称;

newInstance()实例化对象;

getDeclaredFields()获取本类的全部属性;

getMethod()获取类中的方法对象;

Field类:

获取反射字段的方式:getDeclaredField(),getFields();

getName()获取字段名;

getType()获取字段类型;

get()获取字段的值;

set()设置字段的值;

try {

Class stuClass = Class.forName("com.example.homepage.activity.HH");

Field file = stuClass.getDeclaredField("item_tv"); // 通过反射获取item_tv对象

file.setAccessible(true);

TextView textView = (TextView) file.get(this); // 获取textView实例

textView.setText("text4");

file.set(this,textView);

} catch (Exception e) {

e.printStackTrace();

}

}

code

others

一些测试代码

try {

// Field field = R.id.class.getField("header_tv");

Class stuClass = Class.forName("com.jwenfeng.library.pulltorefresh.view.HeadRefreshView");

// Class stuClass = Class.forName("com.jwenfeng.library.pulltorefresh.PullToRefreshLayout");

// Field f = stuClass.getDeclaredField("header_tv");

// f.setAccessible(true);

//

// TextView textView = findViewById(f.getInt(null));

// Log.e("tea",textView.getText().toString());

// textView.setText("zezeze");

// HeadRefreshView view = (HeadRefreshView) stuClass.newInstance();

// Field[] fieldArray = stuClass.getFields(); // 不包括私有参数

// Field[] fieldArray = stuClass.getDeclaredFields(); //私有参数

// for(Field f : fieldArray){

// Log.e("tea", String.valueOf(f));

// }

Field file = stuClass.getDeclaredField("tv");

file.setAccessible(true);

HeadRefreshView view = new HeadRefreshView(this);

TextView nameStr = (TextView) file.get(view);

Log.e("tea", nameStr.getText().toString());

nameStr.setText("tea9");

Method m2 = stuClass.getDeclaredMethod("getView");

View view1 = (View) m2.invoke(view);

TextView textView1 = view1.findViewById(R.id.header_tv);

textView1.setVisibility(View.GONE);

textView1.setText("tea8");

file.set(view1, textView1);

} catch (Exception e) {

e.printStackTrace();

Log.e("tea", "错误了");

}

LINKS

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值