How to create a text input dialog in Android?

What is the best way to create a modal text input dialog for Android?

I want to block until the user enters the text and presses the ok button, and then extract the text from the dialog box, like a modal dialog in awt.

Thanks!

+4
source share
3 answers

Try the following:

final Dialog commentDialog = new Dialog(this); commentDialog.setContentView(R.layout.reply); Button okBtn = (Button) commentDialog.findViewById(R.id.ok); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //do anything you want here before close the dialog commentDialog.dismiss(); } }); Button cancelBtn = (Button) commentDialog.findViewById(R.id.cancel); cancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { commentDialog.dismiss(); } }); 

reply.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <EditText android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:hint="@string/your_comment" android:id="@+id/body" android:textColor="#000000" android:lines="3" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/ok" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/send" android:layout_weight="1" /> <Button android:id="@+id/cancel" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:text="@android:string/cancel" /> </LinearLayout> </LinearLayout> 
+7
source

You can create a custom dialog with an XML layout. And how do you want to block it until the user enters the text and clicks the ok button, you can make it setCancelable(false) .

Check links in Google Search: Dialog with EditText

+2
source

I think you mean the Modal dialog box.

I would say. Take a look at AlertDialog.Builder . As a starter, a newer way is to use DialogFragment , which gives you more control over the dialogue life cycle.

The key method you are looking for is dialog.setCancelable(false);

+1
source

Source: https://habr.com/ru/post/1401332/


All Articles