Artin GH
Artin GH

Reputation: 566

using editText Globally

I'm new in android developing. in my simple project I'm using an editText very often to get the value in my java file and I wanted to declare and find my edit text just one time and after that i Use it in my entire java file. I code like this:

    EditText editText = (EditText) findViewById(R.id.editText);


public void onClick1(View v){
    Editable input = editText.getText();
    this.editText.setText(input+"1");

}
public void onClick2(View v){
    Editable input = editText.getText();
    this.editText.setText(input+"2");

}

but this code close my application unexpectedly when I try to run it. Please let me know your idea about declaring that edit text and using in my functions.

Upvotes: 0

Views: 96

Answers (1)

Ziya ERKOC
Ziya ERKOC

Reputation: 839

DefineEditText editText; right after your class declaration (private is suggested), so, it should look like:

public class MainActivity extends Activity{
     private EditText editText;
     ...
}

and initialize in your onCreate() method

editText = (EditText) findViewById(R.id.editText);

So you can reach that throughtout your class

Upvotes: 2

Related Questions