Button onClick in viewPager?

后端 未结 1 1043

I\'ve got a viewPager, and in second page i\'ve got a button. I want it to do something onClick but it\'s not doing.

I\'ve done on xml file: android:onClick=\"butto

相关标签:
1条回答
  • 2021-01-14 21:36

    I see a few problems with your code:

    1. You should not be doing anything in onSaveInstanceState aside from...saving state. onSaveInstanceState is only called when the activity is about to be paused; it will not be called until then, therefore attaching a listener in there will not do anything. :(
    2. You can't do btn.setOnClickListener(this) unless your ViewPagerProjectActivity implements onClickListener. So you could implement that, or just use the code below.

    Move this code into onCreate after setContentView(R.layout.main):

    Button btn = (Button) findViewById(R.id.button1);
    
    OnClickListener listener = new OnClickListener(){
    
    @Override
    public void onClick(View v) {
        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
    }};
    if (btn != null)
        btn.setOnClickListener(listener);
    
    0 讨论(0)
提交回复
热议问题