按钮单击事件不起作用

时间:2012-12-21 17:03:42

标签: java android

我正在尝试切换视图,但是当我在第二个视图中时,后台事件点击不起作用..我不知道出了什么问题。

请看我的代码并帮助我!

Part1

Part2

public class t extends Activity implements OnClickListener {
   Button volta;

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

      volta = (Button) findViewById(R.id.button2);
      volta.setOnClickListener(this);

   }

   @Override
   public void onClick(View v) {
      if (v == volta) {
         startActivity(new Intent(t.this, MainActivity.class));
      }
   }
}

4 个答案:

答案 0 :(得分:1)

如果您需要两个布局,请使用viewflipper。如果你想分别有两个活动(java类)和两个布局,那么使用:

Intent i = new Intent (this, myClass.class);
startActivity(i);

启动活动而不是setcontentview

所以这里:

public void onClick(View v) {
                       startActivity(new Intent (MainActivity.this, t.class));
                                         OR IN THE CASE OF T.CLASS:
                       startActivity(new Intent (t.this, MainActivity.class));
               }

答案 1 :(得分:1)

你必须覆盖onBackPressed。更改您的MainActivity,如下所示

public class MainActivity extends Activity {

        private boolean goBack = false;           


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



                Button sobre = (Button) findViewById(R.id.button1);
                  sobre.setOnClickListener(new OnClickListener() {
                   public void onClick(View v) {
                           goBack = true;
                           setContentView(R.layout.janela2);
                   }
                   });


        }

        @Override
        public void onBackPressed() {
        //If you have switched to R.layout.janela2 then go back
        if (goBack){
            setContentView(R.layout.activity_main);
            goBack = false;
            return;
            }
        //else do default action
        super.onBackPressed();
        }
}

答案 2 :(得分:1)

只需执行以下代码,我希望它可以帮助您

MainActivity.java

public class MainActivity extends Activity { 

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

            Button sobre = (Button) findViewById(R.id.button1);
            sobre.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {

               Intent intent = new Intent(MainActivity.this, t.class);
               startActivity(intent);
               }
           });
    }
 }

在t.java

public class t extends Activity{

    Button volta;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.janela2);
    }

 @Override
 public void onStop() {
    super.onStop();
    finish();
 }
}

答案 3 :(得分:0)

如果要在应用程序中返回按钮功能,则必须覆盖onBackPressed()方法。即。

public void onBackPressed() {

     Intent start = new Intent(CurrentClass.this,Next_Activity.class);
      startActivity(start);
     finishActivity(0);
 }