如何编程按钮来切换页面?

时间:2014-01-03 19:47:45

标签: android eclipse button layout

我在eclipse中有一个带有两个布局的android应用程序项目。一个有一个按钮,我需要一些帮助来编程该按钮以将布局切换到另一个页面。我看过无数其他线程,但似乎没有任何工作。如果有人可以提供一个万无一失的方法来做到这一点,我将非常感激。 :d

2 个答案:

答案 0 :(得分:1)

更好的是,只使用一个活动,然后根据需要切换视图。

公共类Activity1扩展了Activity {

     View firstV = null;
   View secondV = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    firstV = inflate(R.layout.my_xml_1);
    secondV = inflate(R.layout.my_xml_2);
    setContentView(firstV);
    secondV = inflate(R.layout.my_xml_2);
    addContentView secondB


            findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
                 @Override
                 public void onClick(View v){
                     firstv.setVisible ( View.invisible};
                     secondv.setVisible ( View.visible};


                 }
            });

    }

答案 1 :(得分:0)

这个问题实际上只是做了android开发中最基本的事情之一......使用意图从一个活动转移到另一个活动。

查看一些教程,从长远来看,它会为您提供更好的服务:http://developer.android.com/training/index.html http://www.vogella.com/articles/Android/article.html

编辑 - 所以这更像是一个答案......

活性1

public class Activity1 extends Activity {

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

                findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
                     @Override
                     public void onClick(View v){
                         Intent intent = new Intent(Activity1.this, Activity2.class);
                         startActivity(intent);
                     }
                });

        }

活性2

public class Activity2 extends Activity {

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

my_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

my_xml_2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>