2011-03-03 7 views
0

안녕하세요, 저는 개발자에게 안드로이드에 대한 새로운 개발자입니다. 간단한 대화 상자를 표시하기위한 코드를 작성했습니다.이 대화 상자에서 텍스트 편집 텍스트를 입력했습니다. 텍스트를 입력 한 다음 화면의 방향을 변경했습니다. 편집 텍스트의 가치가 나타나지 않습니다! 안드로이드 대화 방향 문제

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setTitle("Title"); 
alert.setMessage("Message"); 

// Set an EditText view to get user input 
final EditText input = new EditText(this); 
alert.setView(input); 

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 
    String value = input.getText(); 
    // Do something with value! 
    } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
    // Canceled. 
    } 
}); 

alert.show(); 

이 하나가이 문제를 해결할 수 있습니다하시기 바랍니다 다음과 같이

내가 코드를 작성했다?

답변

1

기본적으로 onRetainNonConfigurationInstance 메서드를 재정의해야합니다. 여기를 참조하십시오 : Faster Screen Orientation Change

발췌 :

"활동 클래스가이 메소드가 임의의 객체에게 미래의 자신과 을 전달하는 데 사용할 수 있습니다() onRetainNonConfigurationInstance라는 특별한 방법. 안드로이드는 필요할 때만이 메서드를 호출 할만큼 똑똑합니다. Photostream의 경우 응용 프로그램에서 이 메서드를 사용하여 다운로드 한을 전달했습니다.이미지를 방향 변경시 향후 활동으로 보냅니다. "

1

프라 사드 ... 사용자가 휴대 전화의 방향을 변경할 때 뷰 요소 뷰 상태가 자동적으로 부드러운 죽에 저장되지 않은 ID가없는 경우 EDITTEXT 상자가 ID를 가지고 있지 않습니다. XML 레이아웃을 사용하여 사용자 정의 대화 상자를 만드는 것이 더 나을 것입니다. 그런 다음 편집 텍스트 상자에 ID가 있어야하고 뷰 상태는 소프트 킬에서 자동 저장되어야합니다.

JAL

나는 몇 가지 코드 here 있습니다.

편집 :이 작업을 할 시간이 없어서 간신히 작동하는 Android 문서에서 가져온 프로토 타입 코드입니다. alert_dialog_text_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<EditText android:text="Stateful" 
android:id="@+id/EditText01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</EditText> 
</LinearLayout> 

같은 XML 입술에서 레이아웃/레이아웃을 만든 다음 경고를 만들려면이 레이아웃을 사용하십시오 EDITTEXT 상자가가에 상태를 저장하기 위해 표시되는 ID를 가지고 있기 때문에

AlertDialog.Builder builder= new AlertDialog.Builder(this); 
    LayoutInflater inflater= getLayoutInflater(); 
    final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null); 
    builder.setTitle("About"); 
    builder.setMessage(alertMessage+"Version: "+versionName); 
    builder.setView(myView); 
    AlertDialog alert= builder.create(); 

을 연약한 살인.

+0

고마워요.하지만 이런 식으로 할 수있는 기회가 있습니다. –

+0

@prasad gai 좋아요. 프로토 타입을 추가 할 것입니다. 시작하기 위해 "간신히 작동합니다"코드를 추가 할 것입니다. – JAL

0

이 질문은 오래된 질문이지만 더 간단한 대답을 제공할만한 가치가 있습니다. JAL은 ID를 설정할 필요성에 대해 언급하지만 Java에서 직접 할 수 있습니다. 예를 들어, 위의 원본 코드에 새로운 라인을 추가 : /res/values에서

// Set an EditText view to get user input 
final EditText input = new EditText(this); 
// Id the EditText so the framework will save/restore it for us 
input.setId(R.id.my_id_for_alert_box_inputs); // <-------- New line 
alert.setView(input); 

ids.xml라는 새로운 XML 파일을 만듭니다. 거기에서 우리는 자바에서 사용이 ID를 정의 : 안드로이드 응용 프로그램 프레임 워크가 저장/정의 된 ID가 뷰를 복원하도록되어 있기 때문에

<?xml version="1.0" encoding="utf-8"?> 
<!-- Integer IDs used for tagging Views made in Java programmatically 
    without clashing with XML-defined views. --> 
<resources> 
    <!-- Used to id the input box in a dialog. Reused in different dialogs. --> 
    <item type="id" name="my_id_for_alert_box_inputs" /> 
</resources> 

이 작동합니다. 위의 전체 XML 부분은 깔끔하지만 꼭 필요한 것은 아닙니다. Java View.setId() 호출에 구성된 정수를 연결하여 한 줄 수정으로 만들 수 있습니다.