How to get text from EditText?

前端 未结 7 1501
北恋
北恋 2021-01-01 12:14

The question is quite simple. But I want to know where exactly do we make our references to the gui elements? As in which is the best place to define:

final          


        
7条回答
  •  -上瘾入骨i
    2021-01-01 12:43

    If you are doing it before the setContentView() method call, then the values will be null.

    This will result in null:

    super.onCreate(savedInstanceState);
    
    Button btn = (Button)findViewById(R.id.btnAddContacts);
    String text = (String) btn.getText();
    
    setContentView(R.layout.main_contacts);
    

    while this will work fine:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_contacts);
    
    Button btn = (Button)findViewById(R.id.btnAddContacts);
    String text = (String) btn.getText();
    

提交回复
热议问题