0

I'm taking an Android class based on this book: http://www.deitel.com/Books/Android/AndroidforProgrammers/tabid/3606/Default.aspx

I'm working on the TipCalculator example, though it's been modified by the professor I think (to make it work with the new versions and he got the project most of the way done. It's TipCalculator-partial-layout.zip). I don't understand the concepts of how the stuff in the java file knows what seekbar to listen to. Can someone explain it to me? I've been told it has to do with the id, but I don't understand what that means.

This is a snippet from the main.xml file regarding the seekbar:

<SeekBar
          android:id="@+id/customSeekBar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_span="2"
          >

</SeekBar>

This is a snippet from the main.xml file regarding the seekbar:

(part of onCreate)
// get the SeekBar used to set the custom tip amount
      SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar);
      customSeekBar.setOnSeekBarChangeListener(customSeekBarListener);

Then there's the listener object:

private OnSeekBarChangeListener customSeekBarListener = 
      new OnSeekBarChangeListener() 
   {
      // update currentCustomPercent, then call updateCustom
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
         boolean fromUser) 
      {
         // sets currentCustomPercent to position of the SeekBar's thumb
         currentCustomPercent = seekBar.getProgress();
         updateCustom(); // update EditTexts for custom tip and total
      } // end method onProgressChanged

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) 
      {
      } // end method onStartTrackingTouch

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) 
      {
      } // end method onStopTrackingTouch
   }; // end OnSeekBarChangeListener
4

3 回答 3

1

在这个 xml 中:

<SeekBar
      android:id="@+id/customSeekBar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_span="2"
      >

有一行写着“@+id/customSeekBar”。该行大致意思是“添加一个名为'customseekbar'的android id。

然后,在 java 代码中,您正在调用:

SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar);

其中说:找到 ID 为“customSeekBar”(来自 XML)的 android 视图并将其转换为 SeekBar,因为 findViewById 返回一个更通用的视图。

然后你可以在上面调用你所有的java方法,比如分配监听器。


这有意义吗?

可能值得一看“android 基础知识”、“android 初学者”或“android 入门”一书。

于 2013-06-14T14:17:52.487 回答
0

如果您在布局中放置一个元素(例如SeekBarid )"@+id/customSeekBar",它将在代码中映射到R.id.customSeekBar. 这个“魔法”是由R自动生成的文件完成的。因此findViewById(R.id.customSeekBar)可用于准确到达该布局元素。

R文件(位于/gen文件夹中)可以映射布局元素(称为视图 - 例如R.id.myView)、布局本身 ( R.layout.myLayout)、预定义字符串 ( R.string.my_string) 以及您将在适当时候学习的其他元素。

于 2013-06-14T14:15:03.970 回答
0

如果您更改 XML 文件中的某些内容,则会从中生成 Java 类。如果一个项目有一个 id,你可以查找它。存储所有 id 的类被命名为R

所以如果你执行

SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar);

customSeekBar您正在从类中获取数字 idR并寻找具有该 id 的组件,即您的SeekBar. 在下一行中,您只需使用它SeekBar并为其添加一个侦听器。

android:id="@+id/customSeekBar"声明SeekBar我们之前使用过的你的名字。如果你把它改成例如android:id="@+id/foo"你会得到你SeekBar

SeekBar customSeekBar = (SeekBar) findViewById(R.id.foo);
于 2013-06-14T14:17:07.337 回答