1

我正在 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)?

谢谢

4

6 回答 6

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

于 2013-02-04T11:32:08.153 回答
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"/ >
于 2013-02-04T11:36:38.760 回答
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

于 2017-11-13T05:29:03.370 回答
0

您需要查看 Android 文档:

  • 活动
  • OnClickListener
  • 意图

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

祝你好运。

于 2013-02-04T11:38:51.370 回答
0

试试这个,

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

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

当然你会得到解决方案。

于 2013-02-04T11:51:09.670 回答
0

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

于 2016-08-12T21:35:39.477 回答