<small id='ItYvP'></small><noframes id='ItYvP'>

    <i id='ItYvP'><tr id='ItYvP'><dt id='ItYvP'><q id='ItYvP'><span id='ItYvP'><b id='ItYvP'><form id='ItYvP'><ins id='ItYvP'></ins><ul id='ItYvP'></ul><sub id='ItYvP'></sub></form><legend id='ItYvP'></legend><bdo id='ItYvP'><pre id='ItYvP'><center id='ItYvP'></center></pre></bdo></b><th id='ItYvP'></th></span></q></dt></tr></i><div id='ItYvP'><tfoot id='ItYvP'></tfoot><dl id='ItYvP'><fieldset id='ItYvP'></fieldset></dl></div>

    • <bdo id='ItYvP'></bdo><ul id='ItYvP'></ul>
    1. <legend id='ItYvP'><style id='ItYvP'><dir id='ItYvP'><q id='ItYvP'></q></dir></style></legend>

      <tfoot id='ItYvP'></tfoot>

      viewPager中的按钮onClick?

      时间:2023-10-04

          <bdo id='6QRr1'></bdo><ul id='6QRr1'></ul>

          <small id='6QRr1'></small><noframes id='6QRr1'>

          <tfoot id='6QRr1'></tfoot>
            <tbody id='6QRr1'></tbody>
          <legend id='6QRr1'><style id='6QRr1'><dir id='6QRr1'><q id='6QRr1'></q></dir></style></legend>
          <i id='6QRr1'><tr id='6QRr1'><dt id='6QRr1'><q id='6QRr1'><span id='6QRr1'><b id='6QRr1'><form id='6QRr1'><ins id='6QRr1'></ins><ul id='6QRr1'></ul><sub id='6QRr1'></sub></form><legend id='6QRr1'></legend><bdo id='6QRr1'><pre id='6QRr1'><center id='6QRr1'></center></pre></bdo></b><th id='6QRr1'></th></span></q></dt></tr></i><div id='6QRr1'><tfoot id='6QRr1'></tfoot><dl id='6QRr1'><fieldset id='6QRr1'></fieldset></dl></div>
              1. 本文介绍了viewPager中的按钮onClick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 viewPager,在第二页我有一个按钮.我希望它在 onClick 上做一些事情,但它没有做.

                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.

                我已经完成了 xml 文件:android:onClick="buttonClick"

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

                而且我在 onCreat 内外都尝试过 setOnClickListener...

                And also i've tried setOnClickListener both inside and outside of onCreat...

                这些都没有成功...

                我的 viewPagerAdapter 工作正常!我可以看到页面,并在它们之间切换.但我只是无法让按钮做任何事情.它可以在适配器中完成,但我会做很多事情,所以它不会有用......

                My viewPagerAdapter works fine! I can see pages, and switch between them. But I'm just not able to make a button do anything. It can be done in adapter, but i'll do lots of things so it's not gonna be useful...

                感谢任何帮助.

                代码如下:

                public class ViewPagerProjectActivity extends Activity {
                
                Button btn;
                AbsoluteLayout l;
                
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                
                    ViewPagerAdapter adapter = new ViewPagerAdapter( this );
                    ViewPager pager = (ViewPager)findViewById( R.id.viewpager );
                    pager.setAdapter( adapter );
                    pager.setCurrentItem(0);        
                
                }   
                
                @Override
                protected void onSaveInstanceState(Bundle outState) {
                    super.onSaveInstanceState(outState);
                
                    l = (AbsoluteLayout) findViewById(R.id.layout2);
                    btn = (Button) l.findViewById(R.id.button1);
                    // btn.setOnClickListener(this);        
                }
                
                public void buttonClick(View v) {
                    if(v.equals(btn)) {                 
                        l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
                    }
                }
                

                }

                推荐答案

                我发现你的代码有一些问题:

                I see a few problems with your code:

                1. 除了...保存状态之外,您不应该在 onSaveInstanceState 中做任何事情.onSaveInstanceState 仅在activity即将暂停时调用;在那之前它不会被调用,因此在其中附加一个监听器不会做任何事情.:(
                2. 除非您的 ViewPagerProjectActivity 实现 onClickListener,否则您不能执行 btn.setOnClickListener(this).所以你可以实现它,或者只使用下面的代码.
                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.

                将此代码移动到 setContentView(R.layout.main) 之后的 onCreate 中:

                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);
                

                这篇关于viewPager中的按钮onClick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在片段中使用 ViewPager 下一篇:由于导航回旧片段,查看 Pager Crash Null Pointer Exception - Android

                相关文章

                  1. <small id='JIkyQ'></small><noframes id='JIkyQ'>

                    <tfoot id='JIkyQ'></tfoot>
                    <i id='JIkyQ'><tr id='JIkyQ'><dt id='JIkyQ'><q id='JIkyQ'><span id='JIkyQ'><b id='JIkyQ'><form id='JIkyQ'><ins id='JIkyQ'></ins><ul id='JIkyQ'></ul><sub id='JIkyQ'></sub></form><legend id='JIkyQ'></legend><bdo id='JIkyQ'><pre id='JIkyQ'><center id='JIkyQ'></center></pre></bdo></b><th id='JIkyQ'></th></span></q></dt></tr></i><div id='JIkyQ'><tfoot id='JIkyQ'></tfoot><dl id='JIkyQ'><fieldset id='JIkyQ'></fieldset></dl></div>
                      <bdo id='JIkyQ'></bdo><ul id='JIkyQ'></ul>
                  2. <legend id='JIkyQ'><style id='JIkyQ'><dir id='JIkyQ'><q id='JIkyQ'></q></dir></style></legend>