【问题标题】:android: creating method in onCreate() running in onResume()android:在 onResume() 中运行的 onCreate() 中创建方法
【发布时间】:2013-06-07 18:07:56
【问题描述】:

我在 onCreate() 状态下创建了一个 onClickListener。 程序运行后,我处于 ​​onResume() 状态,为什么我在 onResume() 状态下调用 onClickListener 会起作用?

州之间不应该有区别吗:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button myButton= (Button) findViewById(R.id.button1);
        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView tv= (TextView) findViewById(R.id.textView1);
                tv.setText("CIAO 1");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

【问题讨论】:

  • 你说的“调用onResume()作品中的onClickListener”是什么意思?
  • states 都在谈论什么?这些只是框架为响应事件而调用的异步方法。

标签: android onclicklistener oncreate onresume


【解决方案1】:

当您在 onCreate() 中时,您正在将按钮的 onClick 事件注册到匿名类 View.OnClickListener()

现在这个类有一个方法 (onClick()) 正在等待按钮单击事件发生。

现在想象一下,当特定事件发生时,您要求一个人执行一项任务。

在这种情况下,当按钮单击发生时,您已要求“View.OnClickListener()”人员执行任务“onClick()”。

现在,即使您在 onResume() 中,并且用户按下该按钮,“人”也会收到有关事件的通知并执行任务,即“onClick()”。

因此,一旦您使用 onClickListener 注册按钮,您是在 onCreate 还是 onResume 中都没有关系。

【讨论】:

  • 如果您需要对上述解释的任何澄清,请随时询问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多