2012-02-09 6 views
0

Developer.Androidの例herenewRadioButton.setId(R.id.snack);の機能は何ですか?ラジオボタンの例でR.id.snackは何をしますか?

これらの例では、学習者が何をしているかを推測するのに役立つと思われる変数名を使用しています。では、なぜ彼らはここでスナックと呼ぶのですか? 彼らは私にそれをラジオヒントと考えるように導くために軽食を呼んだのですか?

編集は、彼らが唯一の食べ物をテーマにした変数に

+0

あなたはあなたの質問を詳しく説明できますか。 – Raj

答えて

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

ああ、私は今理解しています。ご協力いただきありがとうございます! –

1

私はちょうど1年以上前の答えを持っていることを知っています。この問題の解決策を追加したいのですが、Ridid.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> 
関連する問題