0

我正在尝试为标题导航创建按钮侦听器,以便侦听器可以位于某个地方。

我希望做出这样的方法:

public void set_nav_listeners ( )
{
    Button planBusiness = (Button)findViewById(R.id.home_header);
    Button questions_header = (Button)findViewById(R.id.questions_header);

    learn_header.setOnClickListener(new Button.OnClickListener() 
{  
    public void onClick(View v) 
    {                   
      Intent myIntent = new Intent(ProblemioActivity.this, LearnActivity.class);
      ProblemioActivity.this.startActivity(myIntent);
    }
});

questions_header.setOnClickListener(new Button.OnClickListener() 
{  
    public void onClick(View v) 
    {                   
      Intent myIntent = new Intent(ProblemioActivity.this, QuestionsActivity.class);
      ProblemioActivity.this.startActivity(myIntent);
    }
}); 
}

并在那里传递参数,使其适用于每个活动。所以像这样的调用会起作用:

public class RandomActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        // For checking if the person is logged in.
        set_nav_listeners ( ---> What parameters can I pass so it would work? <---- );
        ...

那可能吗?我该如何解决这个问题?

谢谢!

4

1 回答 1

1

最好创建一个类并扩展它。

公共类 RandomActivity 扩展 BaseActivity

public class BaseActivity extends Activity {

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);



        findViewById(R.id.home_header).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(BaseActivity.this, "HITESTING!!!",
                        Toast.LENGTH_LONG).show();

            }
        });

    }

}
于 2012-05-26T15:25:38.857 回答