2012-02-09 37 views
0

在Developer.Android示例中herenewRadioButton.setId(R.id.snack);做什么?R.id.snack在Radio Button示例中做了什么?

在这些例子中,他们使用假设的变量名来帮助学习者推断它做了什么。那么他们为什么选择在这里称它为点心? 他们是否称它为零食,试图引导我将其视为无线电提示?

编辑发现他们都称这是一个“快餐”,只是因为前面的例子中使用的食品为主题的变量

+0

你能否详细说明你的问题。 – Raj 2012-02-09 13:47:06

回答

1

我不知道他们为什么叫它点心。 但是什么意思:newRadioButton.setId(R.id.snack); ,他们给它一个ID。 他们作出了关于previousd 2行的单选按钮,当你给它一个ID,您可以使用单选按钮的代码,例如:

//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文件中制作按钮。

1

这条线意味着要设置并为新创建的RadioButton ID,这样你可以参考它稍后在该代码中使用该ID。该名称并不代表一些特别的东西,它可以是任何东西,他们把它命名为小吃因为本例中的其他RadioButton的设置类似于早餐,午餐,晚餐等ID

+0

我现在明白了。谢谢你的帮助! – 2012-02-09 19:11:23

1

我知道这个问题有一个多年前的答案,我只是想为这个问题添加其他解决方案,以修复R.id.snack在来自apidemo的单选按钮示例中,您必须添加名为: 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>