How does findViewById work?

后端 未结 3 817
广开言路
广开言路 2020-12-19 09:08
package com.example.dell.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.v         


        
3条回答
  •  失恋的感觉
    2020-12-19 09:34

    The root view in an Activity is determined by setContentView(int layoutId) or setContentView(View rootView).

    In your case, it is

    setContentView(R.layout.activity_main);
    

    Therefore, any call you make to findViewById will lookup the id from activity_main.xml.

    If it is unable to find the id that you have specified, it will return null.


    It is worth mentioning that that you aren't calling that method and this is typically how a Toast is made.

    Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show();
    

提交回复
热议问题