You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何在AlertDialog回显用户输入内容?

Fixing AlertDialog Input Echo Issue in Android Studio

Hey there! Let's get your AlertDialog to properly show the user's input—this is a super common pitfall, so I’ll walk you through the fixes step by step.

First, Let's Cover the Most Likely Culprits

Before jumping into code, here are the top reasons your dialog isn’t showing input:

  • You’re grabbing the input too early (like when the activity first loads, before the user types anything)
  • You’re not correctly referencing the input field or dialog views
  • For custom dialog layouts, you’re inflating the view wrong or missing the text assignment

Solution 1: Basic AlertDialog (Using Built-in Message)

This is the simplest approach—just pass the user’s input directly into the dialog’s message when the button is clicked.

Step 1: Your Activity Layout (activity_main.xml)

Make sure you have an EditText and a trigger button:

<EditText
    android:id="@+id/userInputEt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type something here..."/>

<Button
    android:id="@+id/showDialogBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Confirmation"/>

Step 2: Activity Code (Kotlin Example)

The key here is to get the input only when the button is clicked—this ensures you’re getting the latest text the user entered:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val inputEt = findViewById<EditText>(R.id.userInputEt)
        val showDialogBtn = findViewById<Button>(R.id.showDialogBtn)

        showDialogBtn.setOnClickListener {
            // Grab the input RIGHT WHEN the user clicks the button
            val userInput = inputEt.text.toString().trim()

            // Build and show the dialog with the input
            AlertDialog.Builder(this)
                .setTitle("Your Input")
                .setMessage("You entered:\n$userInput")
                .setPositiveButton("Got it") { dialog, _ -> dialog.dismiss() }
                .setNegativeButton("Cancel", null)
                .show()
        }
    }
}

Java Version for Reference

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText inputEt = findViewById(R.id.userInputEt);
        Button showDialogBtn = findViewById(R.id.showDialogBtn);

        showDialogBtn.setOnClickListener(v -> {
            String userInput = inputEt.getText().toString().trim();
            
            new AlertDialog.Builder(MainActivity.this)
                .setTitle("Your Input")
                .setMessage("You entered:\n" + userInput)
                .setPositiveButton("Got it", (dialog, which) -> dialog.dismiss())
                .setNegativeButton("Cancel", null)
                .show();
        });
    }
}

Solution 2: Custom AlertDialog Layout

If you need a more customized dialog (like styling the input display), here’s how to do it correctly:

Step 1: Custom Dialog Layout (dialog_input_echo.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/displayInputTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>
</LinearLayout>

Step 2: Activity Code to Use the Custom Layout

showDialogBtn.setOnClickListener {
    val userInput = inputEt.text.toString().trim()

    // Inflate the custom dialog layout
    val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_echo, null)
    // Find the TextView in the custom layout and set the input
    val displayTv = dialogView.findViewById<TextView>(R.id.displayInputTv)
    displayTv.text = "You entered:\n$userInput"

    AlertDialog.Builder(this)
        .setTitle("Confirmation")
        .setView(dialogView)
        .setPositiveButton("OK", null)
        .show()
}

Common Mistakes to Avoid

  • Grabbing input too early: Don’t get the EditText value in onCreate()—that will only capture the empty initial state, not the user’s typed text.
  • Incorrect view references: Double-check that your EditText and dialog view IDs match exactly in XML and code.
  • Forgetting to trim: Using trim() removes extra spaces, but it’s optional—just make sure you’re not accidentally showing empty strings if the user only typed spaces.

Give these fixes a try, and your dialog should start echoing the user’s input perfectly!

内容的提问来源于stack exchange,提问作者Godymn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:42:41