如何使按钮重定向到另一个XML布局

时间:2013-02-04 11:26:08

标签: android xml button

我在xml(res / layout / activity_home.xml)中创建一个按钮,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity" >
    <ImageView
        android:id="@+id/imageView1"
        android:src="@drawable/schkopwide" 
        android:contentDescription="@string/HTI"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="78dp"
        android:onclick="Intent i = new Intent(activity_store.xml);
        startActivity(i);"
        android:text="@string/HTI" />
</RelativeLayout>

那么我应该在这个xml中添加什么来让它重定向到另一个xml页面(res / layout / activity_store.xml)?

谢谢

6 个答案:

答案 0 :(得分:1)

您无法在XML中的onclick参数中添加Intent的启动。你必须通过代码来完成它。

在您的代码中:

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(this, ActivityStore.class);
            startActivity(i);
        }
    });

在ActivityStore类的OnCreate中,放置

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);
    }

注意:我认为你的activity_store类叫做ActivityStore

答案 1 :(得分:1)

试试如下:

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="78dp"
    android:onclick="start"
    android:text="@string/HTI" />

在您的主要活动中:

  Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(this, ActivityStore.class);
        startActivity(i);
    }
});

这是您的ActivityStore类代码:

 public class ActivityStore extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
         }
       }

还要将活动添加到您的mainfest文件中。

<activity
        android:name=".ActivityStore"
        android:label="@string/app_name"/ >

答案 2 :(得分:1)

如果您想在Same Activity中显示两种不同的布局,那么 ViewSwitcher 是最佳布局。

您可以在ViewSwithcher中添加多个布局。并使用 viewswitcher.next(); 函数替换它们。

<ViewSwitcher

android:id="@+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<!--   Add Two View’s Here -- >

</ViewSwitcher>

您可以从以下链接中获取参考:http://abhiandroid.com/ui/viewswitcher

答案 3 :(得分:0)

您需要查看Android文档:

  • 活动
  • OnClickListener
  • 意图

http://developer.android.com/training/index.html

祝你好运。

答案 4 :(得分:0)

试试这个,

在其他XML布局中静态包含XML布局。使用include。在activity_store.xml中添加以下代码

  <include layout="@layout/activity_home"/>

当然,你会得到解决方案。

答案 5 :(得分:0)

一种简单的方法是创建一个附加了另一个xml的Activity,然后使用intent。