2015-03-02 6 views
1

Мне нужна ваша помощь в получении строкового значения при обновлении и отображении его в AlertDialog. Я хотел бы, чтобы скопировать текст в новую строку, на кнопку копирования нажмитеКак получить текст из AlertDialog в Android

и хотите добавить кнопку «COPY» в AlertDialog

String plain ="hello;" // changing on refresh 

new AlertDialog.Builder(Demo.this) 
.setTitle("New Message") 
.setMessage(plain) 
.setPositiveButton(android.R.string.ok, null) 
.setCancelable(false) 
.create().show(); 
+0

http://stackoverflow.com/questions/9978393/android-get-retrieve-progressdialog-titles-id-and-dialegerror-titles-id –

ответ

0

Здравствуйте Пуджа, Вот код для вашей потребности

public class Blah extends Activity { 
private String result; 

// other activity stuff 

void showDialog(){ 

String plain="hello"; 

AlertDialog.Builder b = new AlertDialog.Builder(this); 
b.setTitle("Please enter a password"); 
b.setMessage(plain) 
final EditText input = new EditText(this); 
b.setView(input); 
b.setPositiveButton("OK", new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int whichButton) 
    { 
     // SHOULD NOW WORK 
     result = plain; 
    } 
}); 
b.setNegativeButton("CANCEL", null); 
b.create().show(); 
} 
} 
+0

i dont want input я хочу скопировать из .setMessage – Pooja

+0

привет @Pooja , так просто получить эту строку в onClick-слушателе, то есть в onClick listener result = plain – Mohan

0

Добавление кнопки нейтральной и установите его слушателя, как это:

final String plain ="hello;" // changing on refresh 
String copy; 

new AlertDialog.Builder(Demo.this) 
.setTitle("New Message") 
.setMessage(plain) 
.setPositiveButton(android.R.string.ok, null) 
.setNeutralButton("COPY", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
        copy = plain; 
       } 
      }) 
.setCancelable(false) 
.create().show(); 
2

Если вы хотите, чтобы сообщение из диалогового окна предупреждения попробуйте ниже код

TextView tv = (TextView)YOUR_ALERT_DIALOG.findViewById(android.R.id.message); 
Log.e(this.getClass().getName(), ""+tv.getText()); 

Если сообщение не установлен в вашем диалоге предупреждения он отбросит исключения нулевого указателя.

Смежные вопросы