如何在按钮单击时打开布局(android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10936042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:32:07  来源:igfitidea点击:

How to open layout on button click (android)

android

提问by Yusef Bee

How can I open another layout xml file when I click on a button in main.xml file?

当我单击 main.xml 文件中的按钮时,如何打开另一个布局 xml 文件?

so if I have main.xml which has a button sying click here and I click it, it opens up second.xml file (layout).

所以如果我有 main.xml 有一个按钮 sying click here 然后我点击它,它会打开 second.xml 文件(布局)。

回答by K_Anas

First Create your two layout:

首先创建你的两个布局:

main.xml

主文件

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

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="This is Activity 1" />

       <Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px">
    </Button>    

</LinearLayout>

second.xml

第二个.xml

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

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="This is Activity 2" />

       <Button android:text="Previous"
        android:id="@+id/Button02"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px">
    </Button>    

</LinearLayout>

Second Add your Activity to the manifest file

其次将您的活动添加到清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rr"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity2"></activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

Activity1.java

Activity1.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
}

To switch to Activity2 you have to:

要切换到 Activity2,您必须:

  1. Gets a reference to the button with ID Button01 on the layout using (Button) findViewById(R.id.Button01).

  2. Create an OnClick listener for the button.

  3. And the most important part, creates an “Intent” to start another Activity. The intent needs two parameters: a context and the name of the Activity that we want to start (Activity2.class)
  1. 使用 获取对布局上 ID 为 Button01 的按钮的引用 (Button) findViewById(R.id.Button01)

  2. 为按钮创建一个 OnClick 侦听器。

  3. 最重要的部分是创建一个“意图”来启动另一个活动。Intent 需要两个参数:上下文和我们要启动的 Activity 的名称(Activity2.class)

Activity2.java

Activity2.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        Button next = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }

回答by Thkru

-Inflate the button from the xml
-add an onClickListener on it
-set a new layout in the onClick event

- 从 xml 中充气按钮 - 在其上
添加onClickListener
- 在 onClick 事件中设置新布局

Button btn = (Button) findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v)
{
    MyActivity.setContentView(R.layout.newlayout);
}

});

Something like this should work...

像这样的东西应该工作......

回答by Fede Henze

A Kotlin way:

Kotlin 方式:

-Add the onClick event directly in the designer.

- 直接在设计器中添加 onClick 事件。

  1. Open the activity (Activity1.xml for example) file in the designer mode
  2. Select the button that will trigger the transition
  3. Add the function name on the onClick box of the right panel with all the button properties
  1. 在设计器模式下打开活动(例如Activity1.xml)文件
  2. 选择将触发过渡的按钮
  3. 在右侧面板的 onClick 框中添加函数名称以及所有按钮属性

-Open the Activity .kt file

- 打开活动 .kt 文件

  1. Add the function with the name that you just defined in the designer

    fun openActivity2(view: View) { intent = Intent(view.context,Activity2::class.java) startActivity(intent) }

  1. 使用您刚刚在设计器中定义的名称添加函数

    fun openActivity2(view: View) { intent = Intent(view.context,Activity2::class.java) startActivity(intent) }

Now you have the function linked to the onClick event of your button

现在您已将函数链接到按钮的 onClick 事件