开发者

access object in main class from a OnClickListener - is this a safe way?

开发者 https://www.devze.com 2023-04-03 22:56 出处:网络
I\'m new to android and I have not done any Java programming for probably near on 2 years. I\'m following the tutorials at the android developer resources.

I'm new to android and I have not done any Java programming for probably near on 2 years.

I'm following the tutorials at the android developer resources. More specifically http://developer.android.com/resources/tutorials/views/hello-formstuff.html Using the EditText widget.

Now the code in the example runs fine (creating the listener in the onCreate method)

However, when playing around I like to define the listener elsewhere, simply because it separates the code out to make it easier to read and manage,

So here is my class with all other stuff taken out

public class HelloFormStuf extends Activity {
private EditText edittext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(btnOnClickListener);
    edittext = (EditText)findViewById(R.id.edittext);
    edittext.setOnKeyListener(etOnKeyListener);

}
private OnClickListener btnOnClickListener = new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(HelloFormStuf.this, "Beep Bop", Toast.LENGTH_SHORT).show();
    }
};

private OnKeyListener etOnKeyListener = new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // if the event is a key-down event on the enter button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                (keyCode == KeyEvent.KEYCODE_ENTER)) 
        {
            Toast.makeText(HelloFormStuf.this, edittext.getText() ,Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}; }

Now the original tutorial the edittext is final.

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // If the event is a key-down event on the "enter" button
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
      // Perform action on key press
      Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
      return true;
    }
    return false;
}});

My question is, is this safe to do it this way compared to the tutorial? Because the big difference is now really is that the EditText object is now no longer fina开发者_StackOverflowl and is declared outside the class, but initialized inside the onCreate

Is this safe? such as there will be no little quirks laying around the corner to cause bugs. Does making the EditText object not final really matter?

My button is final, its listener is declared elsewhere and android is happy, I guess because I'm not altering the button or getting any properties of the button. Where as if I keep EditText final, my listener code cannot resolve edittext (The EditText object).

Can I carry on doing this or is there another way (other then declaring it all in the onCreate method).


They make the EditText final so that the reference could not change since you couldn't reassign it to other value. By putting it as an instance variable, a reference to the EditText might change during onKey in

Toast.makeText(HelloFormStuf.this, edittext.getText() ,Toast.LENGTH_SHORT).show();
return true;

Below is the code showing reference reassignment:


// get the EditText from the XML
this.editText = (EditText)this.findViewById(R.id.edittext);
this.editText.setOnKeyListener(etOnKeyListener);

// Create a new EditText
EditText modifiedText = new EditText(this);
modifiedText.setText("Foo Bar");

// reassign the reference to modified text
this.editText = modifiedText;

Now when you press the key, you will see "Foo Bar" in your toast instead of what is in the EditText in the layout

Optionally, you could get the right reference of the View that you are setting the listener to from the View v argument in onKey below

public abstract boolean onKey (View v, int keyCode, KeyEvent event)

Below is the modification of the listener code to get the text from the EditText where the OnKeyListener is listening to


private OnKeyListener etOnKeyListener = new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // if the event is a key-down event on the enter button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
                (keyCode == KeyEvent.KEYCODE_ENTER)) 
        {
            // get the EditText that this listener is set on
            EditText editTextWithListener = (EditText)v;

            // now you will get the text from the EditText where you are listening to even if you
            // change the reference as above
            Toast.makeText(WebViewMain.this, editTextWithListener.getText() ,Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}; 

Note that despite the little risk of having the reference changes, putting your View as instance variable is not an uncommon practice. Many code that I've seen only assign the View once via onCreate like you did thereby reducing the risk of reference reassignment.

0

精彩评论

暂无评论...
验证码 换一张
取 消