我有以下问题。这是我的布局的一个小草图(只有感兴趣的部分):
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/horizontalLayoutContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
现在,我在 HorizontalLayoutContainer ID 布局中添加了四个按钮。我希望能够禁用容器内的按钮,但我不是粗略的:((我怎样才能获得与动态添加的按钮相关的视图?(我不能在按钮上使用 generateID因为它可以从 API 17 中获得)。
我也尝试过手动设置 ID,从 0 开始,结果是我能够捕捉到 ID = 0 而不是其他的 :(( 请帮助
谢谢
编辑: ViewGroup mLayout = (ViewGroup) ((FragmentActivity) mContext).findViewById(rootLayoutID);
if(mLayout!=null){
for(int i = 0; i < mLayout.getChildCount(); i++)
{
View view = mLayout.getChildAt(i);
if(!(view instanceof Button))
setViewsEnabled(mContext, view.getId(), enable);
else{
Log.debug(tag, "I'm a button with ID: "+ view.getId());
view.setEnabled(enable);
view.setClickable(enable);
}
view.setEnabled(enable);
}
mLayout.setEnabled(enable);
}
}
使用这部分代码,我可以禁用除按钮之外的所有内容。如您所见,我还创建了一个打印 4 次的日志(正是按钮的编号),但 ID = 0;
已解决:我找到了一个解决方案,方法是以编程方式(从 0 开始递增)分配一个 ID,然后进行如下所示的 if/else,以便识别定义按钮的正确活动:
/*
* Se l'istanza e' MainActivity disabilito/abilito manualmente i bottoni
*/
if(mContext.getClass() == MainActivity.class){
ViewGroup horizontalScrollViewViewGroup = (ViewGroup)
((FragmentActivity) mContext).findViewById(R.id.horizontalLayoutContainer);
if(horizontalScrollViewViewGroup!=null){
Log.debug(tag, "Buttons Number: "+horizontalScrollViewViewGroup.getChildCount());
for(int i = 0; i < horizontalScrollViewViewGroup.getChildCount(); i++){
View view = ((FragmentActivity) mContext).findViewById(i);
if(view!=null){
view.setClickable(enable);
view.setEnabled(enable);
}
}
}
}