gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:05:34 24 4
gpt4 key购买 nike

我对编码还很陌生,这只是我在社区大学的第二个学期,因此我还有很多东西要学。

在我的 Android 开发类(class)中,我的任务是使用我在此处的 Android 开发类(class)中创建的程序 - https://developer.android.com/training/basics/firstapp/starting-activity#java - 作为新程序的基础,该程序有 2 个文本框,并通过单击 1 发送按钮将用户输入这些文本框的内容显示在显示屏上。

我卡住了。

自上周二发出挑战以来,我一直在做这个,我一直在谷歌搜索、DuckDuckGo 搜索、stackoverflow 搜索、此处搜索 - https://developer.android.com/docs/ - 我尝试了很多不同的方法,但我似乎无法找到如何实现这一点。

!!郑重声明:我并不是要别人为我编写程序/应用程序的代码。我只是想了解如何让这个 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>

最佳答案

您必须实现 onClick 函数并从那里发送 Intent 。

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();
}
}
});

和显示类:

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);

}

另外不要忘记将您的 displayActivity 添加到 AndroidManifest.xml

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

现在您必须为 DisplayMessageActivity 创建第二个用户界面,转到 res/layout,右键单击布局文件夹并创建一个名为 display_activity 的新布局。这是我的 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>

最后这是 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>

关于java - 如何让一个按钮在下一屏显示两个文本框的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54652140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com