ダイアログボックス内のオプションの選択からテキストを取得するテキストビューがあります。選択範囲を別のレイアウトにも表示するにはどうすればよいですか?

Booles_Rules

Screen1のテキストビューを取得してScreen2のテキストビューとして表示しようとしています。Screen1のtextViewは、ダイアログボックスの形式で表示されるチェックボックス項目のユーザーの選択に基づいて設定されます。ボタンがクリックされるとダイアログボックスが表示されます(この場合、ボタンはList1およびList2と呼ばれます)。ダイアログボックス内で、ユーザーには3つの項目が表示され、必要な項目を選択し、[選択肢の追加]を選択すると、ボタンの下に選択内容が表示されます。

ユーザーが「次へ」ボタンをクリックすると、選択内容が次のレイアウト(画面2)に表示されるようにしたいと思います。

ユーザーに提示される選択肢は、res-Stringsファイルに含まれる2つのArrayList(ボタンごとに1つの選択肢の配列)内に記述されています。

正直なところ、私が試したさまざまなことを要約するのは難しいです。レイアウト間でテキストを送信するために説明されているさまざまな方法論を試しながら数日間取り組んできましたが、この特定の状況ではどれもうまくいきませんでした。

私はAndroid開発に不慣れなので、すべての提案がここで役立ちます。

Screen2をコードに含めませんでした。Screen2は、2つの空のテキストビューと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>
Manzurul Hoque Rumi

ダイアログから選択したオプションを格納する文字列変数を作成します。次に、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);
            }
        });

そして、2番目のアクティビティで値を取得します

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ