将编辑文本输入显示为 toast

Displaying edit text input as a toast

我创建了一个自定义警报对话框,其布局包含编辑文本。我想要的是创建一个 Toast 以便在单击警报对话框的 Yes 按钮时编辑文本中的 string显示为 Toast.

的消息

下面是我的代码

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是我的dialog_layout.xml

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

    <EditText
        android:id="@+id/editTextInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:hint="PIN"
        android:inputType="number"
        tools:ignore="HardcodedText" />

</RelativeLayout>

MainActivity.java


import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    AlertDialog alertDialog;
    AlertDialog.Builder builder;
    EditText editTextPIN;
    ListView listView;
    List<String> customList;

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

        listView = findViewById(R.id.myList);

        customList = new ArrayList<>();
        customList.add("George");
        customList.add("Brandon");
        customList.add("Lucas");
        customList.add("Ann");

        editTextPIN = findViewById(R.id.editTextInput);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, customList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:
                    builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Safaricom");
                    builder.setMessage("Enter Scratch Card PIN");
                    builder.setView(R.layout.dialog_layout);
                    builder.setCancelable(false);
                    builder.setPositiveButton("YES", (dialog, which) -> {
                        //I want the String pin to be toasted on clicking the "YES" positive button
                        String pin = editTextPIN.getText().toString();
                        Toast.makeText(this, pin, Toast.LENGTH_SHORT).show();

                    }).setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
                    alertDialog = builder.create();
                    alertDialog.show();
                    return;
                case 1:
                    Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
                    return;
                case 2:
                    Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
                    return;
                case 3:
                    Toast.makeText(this, "pos " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

尝试使用以下代码并更新您的对话框代码。

 public void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Safaricom");
    builder.setMessage("Enter Scratch Card PIN");
    View view = this.getLayoutInflater().inflate(R.layout.dialog_layout, null);
    builder.setView(view);
    builder.setCancelable(false);
    builder.setPositiveButton("YES", (dialog, which) -> {
        //I want the String pin to be toasted on clicking the "YES" positive button
        EditText editTextPIN = view.findViewById(R.id.editTextInput);
        String pin = editTextPIN.getText().toString();
        Toast.makeText(this, pin, Toast.LENGTH_SHORT).show();

    }).setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}
}