2011-03-29 4 views
0

AlertDialog를 사용하여 텍스트를 동 기적으로 가져 오는 방법은 무엇입니까?AlertDialog를 사용하여 텍스트를 동 기적으로 가져 오기

public String GetTextDialog() 
    {   
    final EditText text = new EditText(activity); 

    // Gets the chat 
    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity); 
    dialog.setView(text); 
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
     } 
    }); 
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
     } 
    }); 

    // Display 
    dialog.show(); 

    // Return text after dialog is complete 
    return text.getText().toString(); 
    } 

편집 : 예를 들어,이 같은 일을 할 것 같은

interface TextHandler 
{ 
    public String Title(); 
    public void HandleText(String text); 
} 

public static boolean ShowTextDialog(
     String title, 
     String defaultValue, 
     final TextHandler posButton, 
     final TextHandler negButton, 
     final TextHandler neutButton, 
     int width 
     ) 
{ 
    // Check for already existing dialog 
    if(showDialog) return false; 
    showDialog = true; 

    // Verify context 
    if(context == null) 
    { 
     Log.e("Crystal", "Screen::ShowTextDialog No Context!"); 
     showDialog = false; 
     return false; 
    } 

    // Verify we have buttons 
    if(neutButton == null) 
    { 
     if(posButton == null || negButton == null) 
     { 
      Log.e("Crystal", "Screen::ShowTextDialog Must supply both postive and negative buttons!"); 
      showDialog = false; 
      return false; 
     } 
    } 
    else 
    { 
     if(posButton != null || negButton != null) 
     { 
      Log.e("Crystal", "Screen::ShowTextDialog Can't have neutral button with other type!"); 
      showDialog = false; 
      return false; 
     } 
    } 

    // Create the dialog 
    AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    if(title != null) dialog.setTitle(title); 

    // Create the chat 
    final EditText text = new EditText(context); 
    text.setSingleLine(); 
    if(defaultValue != null) text.setText(defaultValue); 
    if(width != 0) text.setWidth((int)(width*context.getResources().getDisplayMetrics().density)); 

    // Add text to dialog 
    dialog.setView(text); 

    if(posButton != null) 
    { 
     dialog.setPositiveButton(posButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       posButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    if(negButton != null) 
    { 
     dialog.setNegativeButton(negButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       negButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    if(neutButton != null) 
    { 
     dialog.setNeutralButton(neutButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       neutButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    // Display 
    dialog.show(); 

    return true; 
} 

그리고 전화 :

private void GetUsernameDialog() 
{ 
    ShowTextDialog(
      "Enter Username",             // Title 
      username,               // Default value 
      new TextHandler()             // Positive button 
      { 
       public String Title() { return "Ok"; } 
       public void HandleText(String text) { SetUsername(text); } 
      }, 
      new TextHandler()             // Negative button 
      { 
       public String Title() { return "Cancel"; } 
       public void HandleText(String text) { } 
      }, 
      null,                // Neutral button 
      200                 // Width 
     ); 
} 

답변

2

이 더 좋은 방법을 고려겠습니까 메인 UI 스레드가 다른 하나를 기다리면 대화 상자 해지에서 루퍼를 종료하면서 루퍼가있는 대체 스레드에서 대화 상자를 실행할 수 있습니다. 당신이 그렇게한다면 하나님은 단지 새끼 고양이를 죽일지도 모른다. 동기 데이터 액세스가 필요한 이유는 무엇입니까? 이것은 편의성에 관한 것입니까, 아니면 설계상의 이유가 있습니까?

대화 상자 설정 전에 상태에 액세스하는 경우 중첩 된 익명 클래스가 대답입니다. 당신은 이미 위의 발췌 문장에있는 것들을 가지고 있습니다. 중첩 함수의 변수와 중첩 클래스의 멤버에 대해 자동으로 액세스 할 수 있습니다.

+0

사용자가 검색 한 텍스트를 반환 할 수있는 함수가 필요합니다. 더 나은 것이 있다면 AlertDialog를 사용할 필요가 없습니다. –

+0

GUI는 기본적으로 비동기 (이벤트 구동)입니다. 또한 Windows와 달리 Android는 메시지 루프 내에서 메시지 루프를 실행할 방법이 없습니다. 나는 네가 원하는 것을 이해한다. 질문은 - 왜? –

+0

아마 비동기 적으로 코드를 재 설계 할 수있을 것이라고 생각하지만 텍스트를 가져올 수 있도록 호출하기 쉬운 기능을 원했습니다. 그렇게 쉬운 방법이 없다면 다른 방법을 시도해 볼 수 있습니다. –

관련 문제