gpt4 book ai didi

java - 我有一个 TextView ,它从对话框中的选项选择中获取文本。如何让选择也显示在另一个布局中?

转载 作者:太空宇宙 更新时间:2023-11-04 09:51:48 27 4
gpt4 key购买 nike

我正在尝试从 Screen1 获取 TextView 以显示为 Screen2 中的 TextView 。Screen1 中的 TextView 是根据用户对以对话框形式呈现给他们的复选框项目的选择来设置的。单击按钮时将显示该对话框(在本例中,按钮称为 List1 和 List2)。在对话框中,用户会看到三个项目,他们选择所需的项目,选择“添加选项”,他们的选择将显示在按钮下方。

当用户单击“下一步”按钮时,我希望他们的选择显示在下一个布局(Screen2)中。

呈现给用户的选择在 res-Strings 文件中包含的两个 ArrayList(每个按钮一个选择数组)中进行描述。

说实话,很难回顾一下我尝试过的各种事情,因为已经有很多了。我已经为此工作了几天,尝试了在布局之间发送文本所描述的各种方法,但是,在这种特殊情况下,它们都没有为我工作。

我是 Android 开发新手,所以任何和所有建议在这里都有帮助。

我没有在代码中包含 Screen2,Screen2 仅由两个空 TextView 和一个返回 Screen1 的后退按钮组成。

//Screen1

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;

public class Screen1 extends AppCompatActivity {
//Fields for List 1
Button List1;
TextView Text_View_List1;
String[] Item_Choices;
boolean[] Selected_Choices;
ArrayList<Integer> List1_Items = new ArrayList<>();

//Fields for List 2
Button List2;
TextView Text_View_List2;
String[] Item_Choices2;
boolean[] Selected_Choices2;
ArrayList<Integer> List2_Items = new ArrayList<>();

Button Next;

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

List1 = findViewById(R.id.Button1);
Text_View_List1 = findViewById(R.id.Text_View_List1);
Item_Choices = getResources().getStringArray(R.array.List1_Items);
Selected_Choices = new boolean[Item_Choices.length];


//Show a dialog box when List1 button is clicked containing items from ArrayList defined in res-strings

List1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Screen1.this);
builder.setTitle("Options to choose from");
builder.setMultiChoiceItems(Item_Choices, Selected_Choices, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int position, boolean isChecked) {
if (isChecked){
if (!List1_Items.contains(position)){
List1_Items.add(position);
}
}
if (!isChecked){
if (List1_Items.contains(position)){
List1_Items.remove((Integer) position);
}
}
}
});
builder.setCancelable(false);

//Selected choices are added to List2's Text view
builder.setPositiveButton("Add Choices", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String item1 = "";
for (int i = 0; i < List1_Items.size(); i ++){
item1 = item1 + Item_Choices[List1_Items.get(i)];
if ( i != List1_Items.size() -1){//if the selected choice is NOT last, add a comma
item1 = item1 + "," + System.lineSeparator();//Displays chosen items stacked on top of each other rather than side by side (to save screen space)
}
}
Text_View_List1.setText(item1);
}

});
//Closes out the dialog box
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//clear selections from List2's TextView
builder.setNeutralButton("Clear Selections", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < Selected_Choices.length; i ++){
Selected_Choices[i] = false;
List1_Items.clear();
Text_View_List1.setText("");
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});

//Same set up as List1, but uses data from the second ArrayList described in res-strings folder


List2 = findViewById(R.id.Button2);
Text_View_List2 = findViewById(R.id.Text_View_List2);

//Set up the arrays
Item_Choices2 = getResources().getStringArray(R.array.List2_Items);
Selected_Choices2 = new boolean[Item_Choices2.length];

//Show a dialog box when List2 button is clicked containing items from ArrayList defined in res-strings

List2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Screen1.this);
builder.setTitle("Options to Choose from");
builder.setMultiChoiceItems(Item_Choices2, Selected_Choices2, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int position, boolean isChecked) {
if (isChecked) {
if (!List2_Items.contains(position)) {
List2_Items.add(position);
}
if (!isChecked) {
if (List2_Items.contains(position)) {
List2_Items.remove((Integer) position);
}
}
}

}
});
builder.setCancelable(false);
//Selected choices are added to List2's Text view

builder.setPositiveButton("Add Choices", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String item2 = "";
for (int i = 0; i < List2_Items.size(); i++) {
item2 = item2 + Item_Choices2[List2_Items.get(i)];
if (i != List2_Items.size() - 1) { //if the selected choice is NOT last, add a comma
item2 = item2 + "," + System.lineSeparator(); //Displays chosen items stacked on top of each other rather than side by side (to save screen space)

}
}
Text_View_List2.setText(item2);
}

});
//Closes out the dialog box

builder.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//clear selections from List2's TextView

builder.setNegativeButton("Clear Selections", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < Selected_Choices2.length; i++) {
Selected_Choices2[i] = false;
List2_Items.clear();
Text_View_List2.setText("");
}
}
});

AlertDialog dialog = builder.create();
dialog.show();
}
});
Next = findViewById(R.id.Next);
//goes to screen2
Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextLayout = new Intent(Screen1.this, Screen2.class);
startActivity(nextLayout);
}
});
}
}

//XML for Screen1
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".Screen1">

<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="List1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.188"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.343" />

<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="List2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.726"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.343" />

<TextView
android:id="@+id/Text_View_List1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.193"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.445" />

<TextView
android:id="@+id/Text_View_List2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.66"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.446" />

<Button
android:id="@+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>


//Res-Layout(the arrays)
<resources>
<string name="app_name">TestApp</string>

<string-array name="List1_Items">
<item>Choice 1</item>
<item>Choice 2</item>
<item>Choice 3</item>
</string-array>

<string-array name="List2_Items">
<item>Choice x</item>
<item>Choice y</item>
<item>Choice z</item>
</string-array>



</resources>

最佳答案

创建一个字符串变量来存储从对话框中选择的选项。然后在SecondActivity中传递变量

Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextLayout = new Intent(Screen1.this, Screen2.class);
nextLayout.putExtra("position", value);
startActivity(nextLayout);
}
});

并获取第二个 Activity 的值

String value = getIntent().getExtras().getString("position");

关于java - 我有一个 TextView ,它从对话框中的选项选择中获取文本。如何让选择也显示在另一个布局中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640157/

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