小编Blu*_*tar的帖子

关于Android中findViewById(int id)方法的说明

我正在进行Head First Android开发,我对这个方法有点困惑 - > findViewById(int id)

我在文件"activity_find_beer.xml"中有以下按钮:

<Button
android:id="@+id/find_beer"
android:text="@string/find_beer"
android:onClick="onClickFindBeer" />
Run Code Online (Sandbox Code Playgroud)

以及来自FindBeerActivity.java类的以下代码,它将用户选择啤酒并在textview中显示相同的内容.

public class FindBeerActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_find_beer);
    }
    //Call when the button gets clicked
    public void onClickFindBeer(View view) {
        //Get a reference to the TextView
    TextView brands = (TextView) findViewById(R.id.brands);
       //Get a reference to the Spinner
    Spinner color = (Spinner) findViewById(R.id.color);
       //Get the selected item in the Spinner
    String beerType = String.valueOf(color.getSelectedItem());
       //Display the selected item
    brands.setText(beerType);
    }
    } …
Run Code Online (Sandbox Code Playgroud)

java android android-activity

1
推荐指数
1
解决办法
1780
查看次数

标签 统计

android ×1

android-activity ×1

java ×1