-2

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

请查看我的代码并帮助我!

第1部分

第2部分

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

4 回答 4

1

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

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

启动 Activity 而不是 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));
               }
于 2012-12-21T17:08:27.297 回答
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();
        }
}
于 2012-12-21T17:15:46.810 回答
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();
 }
}
于 2012-12-21T17:44:46.517 回答
0

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

public void onBackPressed() {

     Intent start = new Intent(CurrentClass.this,Next_Activity.class);
      startActivity(start);
     finishActivity(0);
 }
于 2012-12-21T17:20:05.727 回答