当前位置:首页 » 安卓系统 » android控件遍历

android控件遍历

发布时间: 2022-04-29 12:46:44

⑴ android中如何遍历界面上的组件被选中

你先得到radioGroup 然后为 确定按钮绑定一个onclick 事件 当点击的时候调用
radioGroup.getCheckedRadioButtonId();
然后通过findViewById()得到被选中的radioButton
最后通过radioButton.getText()得到被选中按钮的内容。
接着在textview中进行显示就是了

⑵ Android 自定义控件开发中 onLayout如何设置里面控件的大小(Android 大神请进……)

ViewGroup在onLayout函数中通过调用其children的layout函数来设置子视图相对与父视图中的位置,具体位置由函数layout的参数决定,当我们继承ViewGroup时必须重载onLayout函数(ViewGroup中onLayout是abstract修饰),然而onMeasure并不要求必须重载,因为相对与layout来说,measure过程并不是必须的,具体后面会提到。首先我们来看下View.java中函数layout和onLayout的源码

public void layout(int l, int t, int r, int b) {
int oldL = mLeft;
int oldT = mTop;
int oldB = mBottom;
int oldR = mRight;
boolean changed = setFrame(l, t, r, b);
if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
if (ViewDebug.TRACE_HIERARCHY) {
ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
}

onLayout(changed, l, t, r, b);
mPrivateFlags &= ~LAYOUT_REQUIRED;

ListenerInfo li = mListenerInfo;
if (li != null && li.mOnLayoutChangeListeners != null) {
ArrayList<OnLayoutChangeListener> listenersCopy =
(ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
int numListeners = listenersCopy.size();
for (int i = 0; i < numListeners; ++i) {
listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
}
}
}
mPrivateFlags &= ~FORCE_LAYOUT;
}

函数layout的主体过程还是很容易理解的,首先通过调用setFrame函数来对4个成员变量(mLeft,mTop,mRight,mBottom)赋值,然后回调onLayout函数,最后回调所有注册过的listener的onLayoutChange函数。
对于View来说,onLayout只是一个空实现,一般情况下我们也不需要重载该函数:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}

接着我们来看下ViewGroup.java中layout的源码:

public final void layout(int l, int t, int r, int b) {
if (mTransition == null || !mTransition.isChangingLayout()) {
super.layout(l, t, r, b);
} else {
// record the fact that we noop'd it; request layout when transition finishes
mLayoutSuppressed = true;
}
}

super.layout(l, t, r, b)调用的即是View.java中的layout函数,相比之下ViewGroup增加了LayoutTransition的处理,LayoutTransition是用于处理ViewGroup增加和删除子视图的动画效果,也就是说如果当前ViewGroup未添加LayoutTransition动画,或者LayoutTransition动画此刻并未运行,那么调用super.layout(l, t, r, b),继而调用到ViewGroup中的onLayout,否则将mLayoutSuppressed设置为true,等待动画完成时再调用requestLayout()。
上面super.layout(l, t, r, b)会调用到ViewGroup.java中onLayout,其源码实现如下:

@Override
protected abstract void onLayout(boolean changed,
int l, int t, int r, int b);

和前面View.java中的onLayout实现相比,唯一的差别就是ViewGroup中多了关键字abstract的修饰,也就是说ViewGroup类只能用来被继承,无法实例化,并且其子类必须重载onLayout函数,而重载onLayout的目的就是安排其children在父视图的具体位置。重载onLayout通常做法就是起一个for循环调用每一个子视图的layout(l, t, r, b)函数,传入不同的参数l, t, r, b来确定每个子视图在父视图中的显示位置。
那layout(l, t, r, b)中的4个参数l, t, r, b如何来确定呢?联想到之前的measure过程,measure过程的最终结果就是确定了每个视图的mMeasuredWidth和mMeasuredHeight,这两个参数可以简单理解为视图期望在屏幕上显示的宽和高,而这两个参数为layout过程提供了一个很重要的依据(但不是必须的),为了说明这个过程,我们来看下LinearLayout的layout过程:

void layoutVertical() {
……
for (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child == null) {
childTop += measureNullChild(i);
} else if (child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
……
setChildFrame(child, childLeft, childTop + getLocationOffset(child),
childWidth, childHeight);
childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);

i += getChildrenSkipCount(child, i);
}
}
}
private void setChildFrame(View child, int left, int top, int width, int height) {
child.layout(left, top, left + width, top + height);
}

从setChildFrame可以看到LinearLayout中的子视图的右边界等于left + width,下边界等于top+height,也就是说在LinearLayout中其子视图显示的宽和高由measure过程来决定的,因此measure过程的意义就是为layout过程提供视图显示范围的参考值。

layout过程必须要依靠measure计算出来的mMeasuredWidth和mMeasuredHeight来决定视图的显示大小吗?事实并非如此,layout过程中的4个参数l, t, r, b完全可以由视图设计者任意指定,而最终视图的布局位置和大小完全由这4个参数决定,measure过程得到的mMeasuredWidth和mMeasuredHeight提供了视图大小的值,但我们完全可以不使用这两个值,可见measure过程并不是必须的。\\

说到这里就不得不提getWidth()、getHeight()和getMeasuredWidth()、getMeasuredHeight()这两对函数之间的区别,getMeasuredWidth()、getMeasuredHeight()返回的是measure过程得到的mMeasuredWidth和mMeasuredHeight的值,而getWidth()和getHeight()返回的是mRight - mLeft和mBottom - mTop的值,看View.java中的源码便一清二楚了:

public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
public final int getWidth() {
return mRight - mLeft;
}

这也解释了为什么有些情况下getWidth()和getMeasuredWidth()以及getHeight()和getMeasuredHeight()会得到不同的值。

⑶ 怎么样进行Android应用的性能分析

对于手机应用性能可以从两方面关注:
1.app产品做好之后必须从每个控件在国内不同的手机品牌和不同系统版本进行兼容性测试,业内也叫遍历测试,所谓的遍历测试是可以移动识别应用的控件从而进行多层次的运行测试,当中包含了安装测试,启动测试,控件遍历测试,最后是卸载测试!
2.兼容性测试,也就是适配测试完成之后需要开始对网络性能进行测试,这里大概有几个方面需要进行的:网络性能测试,元素加载性能测试,网络可用性测试等等!
国内现有的测试周期和测试手段都是通过人工化测试,真正实现自动化又节省时间与人力的只有借助第三方应用性能管理提供商才可以实现!

⑷ android的ViewpPger中,如何遍历得到下面布局问加你中的所有控件,方便对空间进行统一管理

你可创建一个集合来保存你的几个布局:private ArrayList<View> pageViews;
pageViews = new ArrayList<View>();
pageViews.add(v1);
pageViews.add(v2);
pageViews.add(v3);
pagerAdapter = new ViewPagerAdapter(pageViews);
这里创建Adapter的时候把这个集合传进去。
然后:uiControl = new UIControlForIntegrate(handler, OnAppPressedListener,
this);
uiControl.setResource(pageViews);//控制ui的地方也把这个集合传进去。这样它们指向的都是一个对象。你可以很方便的控制UI。或者你没有控制UI的类。直接在你的主类里面也可直接用这个集合里的View.

⑸ Android中,我想遍历一个集合,处理其中每个对象的数据

上传平台是网络操作,所以要开子线程,然后用handler通知主线程处理下一条数据,直到集合里的所有数据处理完毕;

⑹ android怎么样获取当前activity下所有的textview控件

把设置的方法放在一个方法里面,然后把Id当做参数传进去,比如R.id.text1的话,可以这样用一个变量String
baseId="R.id.",这个方法可以写成setConfig(String
strId){String
currentId=baseId+strId;
//后面的代码按下面来
}
然后再用下面的方法
public
static
int
getResourdIdByResourdName(Context
context,
String
ResName){
int
resourceId
=
0;
try
{
Field
field
=
R.drawable.class.getField(ResName);
field.setAccessible(true);
try
{
resourceId
=
field.getInt(null);
}
catch
(IllegalArgumentException
e)
{
log.showLogDebug("IllegalArgumentException:"
+
e.toString());
}
catch
(IllegalAccessException
e)
{
log.showLogDebug("IllegalAccessException:"
+
e.toString());
}
}
catch
(NoSuchFieldException
e)
{
log.showLogDebug("NoSuchFieldException:"
+
e.toString());
}
return
resourceId;
}将currentId作为参数传入就可以了,这时在调用findViewById找,,,
采纳啊,大哥,写了这么多

⑺ Android怎么对控件数组的每一个元素赋值

Android可以遍历每一个控件,使用instanceof判断类型进行相应的赋值。
比如:Button button = new Button(this);
ImageView textView = new ImageView(this);
View[] views = new View[] {button, textView};
for (View itemview : views) {

if (itemview instanceof TextView) {
System.out.println("This is a imageView");
}
if (itemview instanceof Button) {
System.out.println("This is a button");
}
}
但是要注意一下继承关系,比如Button extends TextView。因此Button 也会走TextView的判断方法,因此需要把子类判断放在前面,得到合适的即continue;
for (View itemview : views) {
if (itemview instanceof Button) {
System.out.println("This is a button");
continue
}
if (itemview instanceof TextView) {
System.out.println("This is a TextView");
continue;
}
if (itemview instanceof TextView) {
System.out.println("This is a imageView");
continue;
}

}

⑻ android遍历界面上的所有控件后,如何判断控件是什么类型

可以通过它的类名来判断:v.getClassName() == "Button"
也可以通过instanceof判断:v instanceof Button

public class Main extends Activity

{

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

LinearLayout loginLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);

String pa="";//遍历所有控件

for (int i = 0; i < loginLayout.getChildCount(); i++)

{

View v=loginLayout.getChildAt(i);

//如何判断是Button或者是TextBox

if(){

Object v = tabWidget.getChildAt(i);

if (v instanceof RelativeLayout)

{

}

}

}

}

热点内容
上传网页用什么服务器 发布:2024-05-03 08:57:08 浏览:908
掌握ftp服务器的配置与管理 发布:2024-05-03 08:06:58 浏览:766
服务器搭建的函数 发布:2024-05-03 07:54:44 浏览:815
php包含数组 发布:2024-05-03 07:53:51 浏览:702
短暂记忆存储信息是有限的 发布:2024-05-03 07:48:14 浏览:537
java集合对象 发布:2024-05-03 07:32:13 浏览:916
苹果自带脚本 发布:2024-05-03 07:16:04 浏览:569
商城导航源码 发布:2024-05-03 07:14:15 浏览:552
shell脚本日志输出 发布:2024-05-03 06:31:04 浏览:713
服务器快捷方式是什么意思 发布:2024-05-03 06:28:18 浏览:108