【问题标题】:Changing Activities through a button and display xml通过按钮更改活动并显示 xml
【发布时间】:2018-06-20 19:35:51
【问题描述】:

我是 android 开发的新手,我正在尝试制作一个在其主要活动中具有 4 个按钮的应用程序,当我单击其中一个按钮时,它会将我带到另一个活动并显示其 xml 文件,应该我在第二个活动中写?到目前为止,这是我的代码。

主 xml 文件

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

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:background="@color/colorAccent"
        android:text="Overview"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Information" />
        <TableRow
            android:id="@+id/hr1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>

        <Button
            android:id="@+id/two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Education" />
        <TableRow
            android:id="@+id/hr2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>
        <Button
            android:id="@+id/three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Work Experience" />
        <TableRow
            android:id="@+id/hr3"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>
        <Button
            android:id="@+id/four"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:text="Education" />
        <TableRow
            android:id="@+id/hr4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#444">

        </TableRow>


    </LinearLayout>

    </LinearLayout>

.......................

 main activity
    package com.lakshay.display.piechart;


    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener {
    Button btn1 , btn2 ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

    }


     @Override
     public void onClick(View v) {
        Intent intent = new Intent();
        String nextAct = null ;
        String shield = "com.lakshay.display.piechart";
        Integer flag= -1;
        switch (v.getId())
        {
            case (R.id.one ):
            nextAct = shield + "ContactActicity";
            break;

            default:
                Toast.makeText(MainActivity.this , "Item Currently Unavailable" 
    , Toast.LENGTH_SHORT).show();

        }
        try {
            if (nextAct!=null)
            {
                intent = new Intent(MainActivity.this , Class.forName(nextAct));
                flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
                if (flag  != -1 ){
                    intent.setFlags(flag);
                } startActivity(intent);
            }
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    }

....... 第二个活动的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    tools:context="com.lakshay.display.piechart.ContactActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Name"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>


    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:inputType="text"
        android:paddingBottom="20dp"


        android:paddingLeft="20dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Address"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:id="@+id/address"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:background="@android:color/background_light"



        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Phone Number"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:id="@+id/number"
        android:paddingBottom="20dp"
        android:background="@android:color/background_light"

        android:paddingLeft="20dp"

    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textStyle="bold"
        android:paddingLeft="20dp"
        android:textSize="22dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:id="@+id/email"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:background="@android:color/background_light"


        />



</LinearLayout>

.......

【问题讨论】:

  • 您遇到错误了吗?你到底想做什么?
  • 基本上我的主要活动有 4 个按钮,即;人员信息,当我单击时,我想转到另一个活动,它显示第二个活动的 xml 文件。我不知道第二个活动要写什么或做什么
  • 请看我的回答。

标签: android xml android-activity


【解决方案1】:

我的答案作为一个清单:

1.- 如果您使用的是 android studio,您应该使用助手创建第二个活动,这样您就不会遇到更多复杂情况。 第二个活动必须有一个 xml 文件和一个类文件。

2.- 您应该在活动的 xml 文件中为您的按钮添加 android:onClick 属性。

3.- 你的第二个活动必须在类文件中有一个onCreate 方法来填充第二个活动的内容。

3b.- 您可以将 onCreate 保留为默认内容,但您的 xml 文件必须包含您的文本视图的所有信息。 如果需要,我可以进一步详细说明。

【讨论】:

    【解决方案2】:

    假设您有两个活动:Activity1.java 和 Activity2.java。

    从 Activity1 启动 Activity2 只需:

    最终 Intent 意图 = new Intent(this, Activity2.class);>
    startActivity(intent)

    如果您想在按钮单击时启动活动,您必须在 onClickListener 中编写此代码。为此,请在 Activity1 xml 文件中的按钮定义中写入以下属性。

    android:onCLick="onButtonClick"

    然后在您的活动中编写以下侦听器:

    public void onButtonClick(final View v) {
    // 把你的意图放在这里
    }

    【讨论】:

      【解决方案3】:
      This code help you ....   
      
       public class MainActivity extends AppCompatActivity {
               Button btn1 ;
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  button = (Button)findViewById(R.id.btn1);
                  final String name = editText.getText().toString();
                  button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
                      public void onClick(View v) {
                       Intent intent = new Intent(this, Activity2.class);//this is call your second activity
                       startActivity(intent);//start activity
                      }
                  });
              }
          }
      

      【讨论】:

        【解决方案4】:

        首先,您必须从您的 xml 中获取您的 Button id。或者你会得到NullPointerException,所以像这样改变你的onCreate

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn1 = (Button)findViewById(R.id.one); //This line
            btn2 = (Button)findViewById(R.id.two); //and this line
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
        }
        

        如果你想用Class调用intent,可以看this解决方案


        您可以像这样简单地调用另一个活动:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn1 = (Button)findViewById(R.id.one); //This line
            btn2 = (Button)findViewById(R.id.two); //and this line
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(firstActivity.this, secondActivity.class));
                }
            });
        }
        

        【讨论】:

          猜你喜欢
          • 2021-09-13
          • 1970-01-01
          • 2013-08-09
          • 1970-01-01
          • 1970-01-01
          • 2022-01-11
          • 1970-01-01
          • 2014-09-03
          • 1970-01-01
          相关资源
          最近更新 更多