當前位置:首頁 » 安卓系統 » 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)

{

}

}

}

}

熱點內容
119濃度的鹽酸怎麼配置 發布:2024-04-20 06:23:38 瀏覽:119
資料庫pf 發布:2024-04-20 05:57:36 瀏覽:393
c語言編譯出現連接錯誤 發布:2024-04-20 05:42:18 瀏覽:198
湖北戴爾伺服器維修系統雲主機 發布:2024-04-20 05:36:47 瀏覽:62
android上傳數據 發布:2024-04-20 05:36:43 瀏覽:142
python替換文本內容 發布:2024-04-20 05:21:22 瀏覽:899
urllib3源碼 發布:2024-04-20 05:11:23 瀏覽:34
如何通過運行打開伺服器文件 發布:2024-04-20 00:06:50 瀏覽:671
電腦百度網盤怎麼上傳 發布:2024-04-19 23:49:15 瀏覽:417
陸放四驅買哪個配置 發布:2024-04-19 23:49:08 瀏覽:407