gpt4 book ai didi

android - 如何在 Android 上获取 parent ID?

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:19 26 4
gpt4 key购买 nike

我试图通过

获取RadioButton的父id(RadioGroup的id)
RadioGroup rg = (RadioGroup) arg0.getParent();

但是失败了。后续施工

RadioGroup rg = (RadioGroup) findViewById(R.id.radiogr);

效果很好。如何在运行时获取父 ID?

因这些日志而失败:

12-26 17:11:05.205: E/AndroidRuntime(912): FATAL EXCEPTION: main
12-26 17:11:05.205: E/AndroidRuntime(912): java.lang.ClassCastException: android.widget.LinearLayout
12-26 17:11:05.205: E/AndroidRuntime(912): at com.letmedraw.shopcart.ShopCartActivity.onCheckedChanged(ShopCartActivity.java:96)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup.access$600(RadioGroup.java:52)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.toggle(CompoundButton.java:86)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.RadioButton.toggle(RadioButton.java:69)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.widget.CompoundButton.performClick(CompoundButton.java:98)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.view.View$PerformClick.run(View.java:8816)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Handler.handleCallback(Handler.java:587)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Handler.dispatchMessage(Handler.java:92)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.os.Looper.loop(Looper.java:123)
12-26 17:11:05.205: E/AndroidRuntime(912): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-26 17:11:05.205: E/AndroidRuntime(912): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:11:05.205: E/AndroidRuntime(912): at java.lang.reflect.Method.invoke(Method.java:521)
12-26 17:11:05.205: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-26 17:11:05.205: E/AndroidRuntime(912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-26 17:11:05.205: E/AndroidRuntime(912): at dalvik.system.NativeStart.main(Native Method)

最佳答案

android中所有的 View (控件)都是View类型的,所以可以使用getId()来获取id。

int parentId = ((View) arg0.getParent()).getId();

关于android - 如何在 Android 上获取 parent ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com