简体   繁体   English

Android-OnClickListener-构造函数未调用

[英]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! 通常我可以通过Gogle找到答案,通常无论如何都会使我陷入stackoverflow,但这最终使我走得足够远,无法提出自己的问题,可能很明显以前没有人问过它!

Anyways, this isn't for anything in particular. 无论如何,这不是特别适合任何事情。 Just trying to get a handle on onlicklisteners . 只是试图掌握onlicklisteners In this case, specifically inner-class onclicklisteners . 在这种情况下,尤其是内部类onclicklisteners

Problem: Why is the program skipping over the constructor for MathButtonClicked ? 问题:为什么程序跳过了MathButtonClicked的构造MathButtonClicked I was it a CharSequence , but the constructor never gets called (I know this via the logcat). 我是一个CharSequence ,但是从未调用过构造函数(我通过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. 另外,我要完成的工作是将EditText视图中的文本传递给新的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 . 我可以通过在OnClick创建EditText对象来做到这一点,但是我想在main方法中创建它,然后将信息传递给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);

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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