【问题标题】:How to make a button redirect to another XML layout如何使按钮重定向到另一个 XML 布局
【发布时间】:2013-02-04 11:26:08
【问题描述】:

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

谢谢

【问题讨论】:

  • 通过教程和开发者文档而不是发布问题... :)

标签: android xml button


【解决方案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】:

    您不能在 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

    【讨论】:

    【解决方案3】:

    如果您想在同一 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

    【讨论】:

      【解决方案4】:

      您需要查看 Android 文档:

      • 活动
      • OnClickListener
      • 意图

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

      祝你好运。

      【讨论】:

        【解决方案5】:

        试试这个,

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

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

        你一定会得到解决的。

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2013-01-17
            • 2019-07-09
            • 2021-07-28
            • 2014-06-03
            • 1970-01-01
            • 1970-01-01
            • 2022-11-14
            • 2020-08-20
            • 1970-01-01
            相关资源
            最近更新 更多