在Developer.Android示例中here newRadioButton.setId(R.id.snack);
做了什么?
在这些示例中,他们使用变量名称来帮助学习者推断它的作用。那他们为什么选择在这里称它为小吃呢? 他们把它称为小吃是否试图引导我将其视为无线电提示?
编辑发现他们称之为“小吃”只是因为上一个例子使用食物主题变量
答案 0 :(得分:1)
该行表示您正在为新创建的RadioButton
设置和ID,以便您可以稍后在代码中通过该ID引用它。这个名字并不代表特别的东西,它可以是任何东西,它们将它命名为 snack ,因为在示例中,其他RadioButton
被设置为ID,如早餐,午餐,晚餐等。
答案 1 :(得分:1)
我不知道为什么他们称它为零食。
但是它的意思是:newRadioButton.setId(R.id.snack);
他们给了它一个ID。
他们在之前的2行上制作了radiobutton,当你给它一个ID时,你可以在代码中使用radiobutton,例如:
//Making RadioButton
RadioButton newRadioButton = new RadioButton(this);
//Give the RadioButton an ID
newRadioButton.setId(R.id.snack);
//making a variable called snack_choice
RadioButton snackchoice;
//Assign an radiobutton to the variable
snack_choice = (RadioButton) findViewById(R.id.snack);
//Use it
if (snack_choice.isChecked()) {
//do something
}
我希望你理解它。
但是,我更喜欢在XML文件中创建按钮。
答案 2 :(得分:1)
我知道这个问题有一年多以前的答案,我只想为这个问题添加其他解决方案,修复来自apidemo的单选按钮示例中的R.id.snack,你必须添加文件,名为:ids .xml在您的值文件夹中并输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<item type="id" name="snack" />
</resources>