简体   繁体   English

如何让一个按钮在下一个屏幕上显示两个文本框的内容?

[英]How can I make one button display the contents of two text boxes on the next screen?

I am very new to coding, this being only my 2nd semester in community college and therefore I have a lot to learn.我对编码很陌生,这只是我在社区大学的第二个学期,因此我有很多东西要学。

In my Android Development course, I have been tasked to use the program I've created from the Android Development lesson here - https://developer.android.com/training/basics/firstapp/starting-activity#java - as the basis for a new program which instead has 2 textboxes and which displays the content the user inputs into those textboxes, onto the display screen, by clicking the 1 send button.在我的 Android 开发课程中,我的任务是使用我在此处的 Android 开发课程中创建的程序 - https://developer.android.com/training/basics/firstapp/starting-activity#java - 作为基础对于一个新程序,它有 2 个文本框,并通过单击 1 发送按钮将用户输入到这些文本框中的内容显示到显示屏上。

I am stuck.我被困住了。

I've been at this since last Tuesday when the challenge was issued and I've been Google searching, DuckDuckGo searching, searching through stackoverflow, searching through here - https://developer.android.com/docs/ - and I've tried many different approaches but I can't seem to find out how to accomplish this.自从上周二发出挑战以来,我一直在做这个,我一直在谷歌搜索,DuckDuckGo 搜索,通过 stackoverflow 搜索,在这里搜索 - https://developer.android.com/docs/ - 我已经尝试了许多不同的方法,但我似乎无法找到如何实现这一点。

!!For the record: I'm not asking someone to code the program/app for me. !!声明:我不是要别人为我编写程序/应用程序。 I'm simply trying to understand how to get this 1 button to send 2 textbox contents to the display - I want to learn!!我只是想了解如何让这个 1 按钮将 2 个文本框内容发送到显示器 - 我想学习!!

//* activity_main.xml */

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/ColorActivity"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:gravity="center"
        android:layout_toRightOf="@+id/Number"
        android:hint="edit_name_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_below="@+id/Name"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_centerHorizontal="true"
        android:text="button_send"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Number" />

    <EditText
        android:id="@+id/Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:gravity="center"
        android:hint="edit_phone_message"
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Name" />
</RelativeLayout>
//* DisplayMessge.java */

package com.example.testapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.example.testapp.R;

public class DisplayMessage extends Activity {

    private TextView name;
    private TextView number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_activity);

        name = findViewById(R.id.NameText);
        number = findViewById(R.id.NumberText);

        Intent getText = getIntent();
        String TheName = getText.getStringExtra("Name");
        String TheNumber = getText.getStringExtra("Number");

        name.setText(TheName);
        number.setText(TheNumber);
    }
}
//* MainActivity.java */

package com.example.testapp;

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

import com.example.testapp.DisplayMessage;
import com.example.testapp.R;

public class MainActivity extends AppCompatActivity {


    Button button;
    private EditText name;
    private EditText number;

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

        //initialization of the EditText and the Button
        name = (EditText) findViewById(R.id.Name);
        number = (EditText) findViewById(R.id.Number);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);

                String mName = name.getText().toString();
                String mNumber = number.getText().toString();
                //Checking if the Entries are empty
                if (mName != null && mNumber != null) {
                    intent.putExtra("Name", mName);
                    intent.putExtra("Number", mNumber);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "Text Entries Missing", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
//* AndroidManifest.xml */

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DisplayMessage">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
            </intent-filter>
        </activity>
    </application>

</manifest>
//* display_activity.xml */

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


    <TextView
        android:layout_gravity="center"
        android:id="@+id/NameText"
        android:layout_weight="1"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/NumberText"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

You have to implement the onClick function and send the intent from there. 您必须实现onClick函数并从那里发送意图。

public class MainActivity extends AppCompatActivity {


Button button;
private EditText name;  
private EditText number;

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

    //initialization of the EditText and the Button
     name = (EditText) findViewById(R.id.Name);
     number = (EditText) findViewById(R.id.Number);
     button = findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);

            String mName = name.getText().toString();
            String mNumber = number.getText().toString();
            //Checking if the Entries are empty
            if(mName!=null&&mNumber!=null) {
                intent.putExtra("Name", mName);
                intent.putExtra("Number", mNumber);
                startActivity(intent);
                finish();
            }else{
                Toast.makeText(getApplicationContext(),"Text Entries Missing",Toast.LENGTH_SHORT).show();
            }
        }
    });

and the Display Class: 和显示类别:

public class DisplayMessage extends Activity {

 private TextView name;
 private TextView number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_activity);

    name = findViewById(R.id.NameText);
    number = findViewById(R.id.NumberText);

    Intent getText = getIntent();
    String TheName =getText.getStringExtra("Name");
    String TheNumber =  getText.getStringExtra("Number");

    name.setText(TheName);
    number.setText(TheNumber);

}

Also don't forget to add your displayActivity to the AndroidManifest.xml 同样不要忘记将displayActivity添加到AndroidManifest.xml中

 <activity android:name=".DisplayMessage">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"></action>
        </intent-filter>
    </activity>

Now you have to create a second user interface for the DisplayMessageActivity go to res/layout, right click on the layout folder and create a new layout named display_activity. 现在,您必须为DisplayMessageActivity创建第二个用户界面,转到res / layout,右键单击布局文件夹,然后创建一个名为display_activity的新布局。 This is my code for the display_activity.xml: 这是我的display_activity.xml代码:

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


<TextView
    android:layout_gravity="center"
    android:id="@+id/NameText"
    android:layout_weight="1"
    android:textSize="18sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/NumberText"
    android:layout_gravity="center"
    android:textSize="18sp"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Lastly this is the activity_main layout: 最后,这是activity_main布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/ColorActivity"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
    android:id="@+id/Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:gravity="center"
    android:layout_toRightOf="@+id/Number"
    android:hint="edit_name_message"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:layout_below="@+id/Name"
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_centerHorizontal="true"
    android:text="button_send"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Number" />

<EditText
    android:id="@+id/Number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:gravity="center"
    android:hint="edit_phone_message"
    android:inputType="phone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Name" />
 </RelativeLayout>

You can simply do that, 您可以简单地做到这一点,

public class MainActivity extends AppCompatActivity {

Button btn;
EditText number, name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    number = (EditText) findViewById(R.id.Number);
    name = (EditText) findViewById(R.id.Name);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String message = name.getText().toString();
            String message2 = number.getText().toString();
            Intent intent = new Intent(this, DisplayMessageActivity.class);
            intent.putExtra("Message", message);
            intent.putExtra("Message2", message2);
            startActivity(intent);
        }
    });
  }
}

and you have to add DisplayMessageActivity class to that also, 并且您还必须在其中添加DisplayMessageActivity类,

String message1= getIntent().getStringExtra("Message");
String message2= getIntent().getStringExtra("Message2");

Just reduce your two functions (snedNumber and sendName) to only one function. 只需将两个函数(snedNumber和sendName)减少为一个函数即可。 Inside this function you get the text of both textfields and put them into your intentextra. 在此函数内,您将获得两个文本字段的文本,并将它们放入intentextra中。 then simply start your new activity 然后只需开始您的新活动

Do it like this 像这样做

public void sendInfo(View view) {

Intent intent = new Intent(this, DisplayMessageActivity.class); 
EditText name = (EditText) findViewById(R.id.Name); 
String message = name.getText().toString(); 
EditText number = (EditText) findViewById(R.id.Number); 
String message2 = number.getText().toString();
intent.putExtra("firstString", message); 
intent.putExtra("secondString", message2); 
startActivity(intent); 
}

And in your DisplayMessageActivity class onCreate method get it like 并在您的DisplayMessageActivity类的onCreate方法中将其获取为

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
    Bundle extra = getIntent().getExtras();
            if (extra != null){
                String message = extra.getString("firstString");
                String message2 = extra.getString("secondString");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存多个编辑文本框 - How can i save more than one edit text boxes 如何通过单击按钮显示此文件的内容? - How can I display contents of this file through the click of a button? 如何让按钮从第一个JTabbedPanel移动到下一个? - How can I make the button move from the first JTabbedPanel to the next one? 我正在尝试用Java创建基于文本的GUI游戏。 如何使消息框更新以显示下一个结果? - I'm trying to make a text based GUI game in Java. How do I make the message boxes update to show the next outcomes? 如何在Android中的RelativeLayout中以编程方式在按钮旁边显示文本视图? - How can i display programmatically a textview next to a button in a RelativeLayout in Android? 一旦用户按下下一个按钮,如何显示下一个文本行? - How to display the next text lines once user presses the next button? 如何使用“+”或“-”按钮进行计算,然后使用“=”显示? - How can i use the "+" or "-" button to make the calcul and then the "=" to display it? 我如何显示以下json的内容 - How I can display the contents of the following json 如何显示void方法的内容? - How Can I display the contents of void method? 如何显示LinkedList的所有内容? - How can I display all the contents of the LinkedList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM