简体   繁体   中英

Android - OnClickListener - Constructor Not Being Called

This is my first question. Usually I can find my answers through Gogle which usually just brings me to stackoverflow anyways, but this finally pushed me far enough to ask my own question, probably so obvious nobody needed to ask it before!

Anyways, this isn't for anything in particular. Just trying to get a handle on onlicklisteners . In this case, specifically inner-class onclicklisteners .

Problem: Why is the program skipping over the constructor for MathButtonClicked ? I was it a CharSequence , but the constructor never gets called (I know this via the logcat).

public class Main extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button mathButton = (Button) findViewById(R.id.mathButton);
    EditText et = (EditText) findViewById(R.id.inputText);
    CharSequence inputText = et.getText().toString();


    mathButton.setOnClickListener(new MathButtonClicked(inputText));
}

private class MathButtonClicked implements OnClickListener {
    private CharSequence receivedText;

    public MathButtonClicked(CharSequence inputText) {
        this.receivedText=inputText;
        Log.d("Constructor", " " + receivedText);
    }



    public void onClick(View v) {
        Log.d("Onclick", " " + receivedText);
        Intent intent = new Intent(Main.this, Math.class);
        intent.putExtra("inputText", getText());
        startActivity(intent);          
    }

    public CharSequence getText(){
        return receivedText;
    }
    }//end inner class
} //end main

Aren't constructors always called if the arguments match the parameters? @_@

Also, what I'm trying to accomplish is pass the text in the EditText view to a new intent.

I can do this by creating the EditText object in the OnClick , but I want to create it in the main method and then pass the information to the OnClick .

Thanks!

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button mathButton = (Button) findViewById(R.id.mathButton);
EditText et = (EditText) findViewById(R.id.inputText);
CharSequence inputText = et.getText().toString();



 mathButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                     String receivedText=inputText;
         Log.d("Onclick", " " + receivedText);
                     Intent intent = new Intent(getApplicationContext(), Math.class);
                     intent.putExtra("inputText", receivedText);
                     startActivity(intent);

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM