2012-03-22 6 views
1

미리 감사드립니다.화면 방향을 변경하면 Edittext가 사라 집니까?

이것은 내 샘플 코드입니다. 언제든지 그것을 실행하고 화면 방향을 변경하려고하면 편집 텍스트가 사라지고 응용 프로그램을 다시 시작해야합니다.

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ed1=(EditText)findViewById(R.id.ed1); 
    ed2=(EditText)findViewById(R.id.ed2); 

    ed1.addTextChangedListener(new TextWatcher()       
    { 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
     } 

     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
     } 

     public void afterTextChanged(Editable arg0) {  
      ed2.setVisibility(View.INVISIBLE); 
     } 
    }); 

    ed2.addTextChangedListener(new TextWatcher()    
    { 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

     }  

     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
     } 

     public void afterTextChanged(Editable arg0) { 
      ed1.setVisibility(View.INVISIBLE); 
     } 
    }); 
} 

을 좀 도와주십시오 내 main.xml에

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TableLayout 
    android:id="@+id/table" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="25dip" 
    android:stretchColumns="0,1" > 

    <TableRow> 

     <TextView 
      android:id="@+id/text1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dip" 
      android:text="Edit Text 1" 
      android:textSize="15dip" /> 

     <EditText 
      android:id="@+id/ed1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="20dip" 
      android:maxLength="10" 
      android:numeric="integer|decimal" /> 
    </TableRow> 

    <TableRow> 

     <TextView 
      android:id="@+id/or_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dip" 
      android:text="OR" 
      android:textSize="20dip" /> 
    </TableRow> 

    <TableRow> 

     <TextView 
      android:id="@+id/text2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dip" 
      android:text="Edit Text2" 
      android:textSize="15dip" /> 

     <EditText 
      android:id="@+id/ed2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dip" 
      android:layout_marginTop="20dip" 
      android:maxLength="10" 
      android:numeric="integer|decimal" /> 
    </TableRow> 
</TableLayout> 

+0

show layout main.xml 파일 –

+0

나는 그것을 추가하시기 바랍니다. – Shri

+0

main.xml에 대한 land-scape 레이아웃이 있습니다 –

답변

2

당신이 당신의 장치를 활동 다시 시작을 회전 할 때마다, 그건 당신이 당신의 EditTexts가 표시되지 않는 이유입니다.

런타임은 응용 프로그램의 변경 사항에 대한

android:configChanges="orientation|keyboardHidden" 

은 또한 더 많은 것을 알고 this을 확인 매니페스트 파일에 사용자의 작업에 XML이 줄을 추가합니다.

+0

이 작동합니다. 감사합니다 – Shri

+0

당신은 환영합니다 ... –

0

void afterTextChanged(Editable) 방법은 당신의 TextWatchers에서 호출되는 것입니다. 다른 방법으로, 예를 들어 onResume() 일 수 있습니다. 프로그래밍 방식으로 EditText의 텍스트를 설정합니까? 이것이 그 원인 일 수 있습니다. 이 경우 이전 상태로 복원하는 위치에 다시 표시하면됩니다.

+0

답변을 시도했지만 편집 텍스트가 사라졌습니다. 값을 지우는 데 settext를 사용했지만 방향이 바뀌어도 성공하지 못했습니다. – Shri

0

하이의 '에서 onCreate'방법 업데이트

ed1.setVisibility(View.VISIBLE); 
ed2.setVisibility(View.VISIBLE); 

의 끝에 다음 행을 추가하려고 :

이 시도, 어쩌면 당신을 도울 것입니다.

public class MyActivity extends Activity { 
    [...] 
    private final boolean isEd1FirstChange = true; 
    private final boolean isEd2FirstChange = true; 
    [...] 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     isEd1FirstChange = true; 
     isEd2FirstChange = true; 

     ed1=(EditText)findViewById(R.id.ed1); 
     ed2=(EditText)findViewById(R.id.ed2); 

     ed1.addTextChangedListener(new TextWatcher()       
     { 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      public void afterTextChanged(Editable arg0) {  
       if (isEd1FirstChange) { 
        isEd1FirstChange = false; 
        return; 
       } 

       ed2.setVisibility(View.INVISIBLE); 
      } 
     }); 

     ed2.addTextChangedListener(new TextWatcher()       
     { 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
      } 

      public void afterTextChanged(Editable arg0) {  
       if (isEd2FirstChange) { 
        isEd2FirstChange = false; 
        return; 
       } 

       ed1.setVisibility(View.INVISIBLE); 
      } 
     }); 

}

+0

아니요, 여전히 두 edittext가 사라집니다. – Shri

+0

내 업데이트를보세요 –

+0

대단히 고마워요, 마침내 나를 위해 일했습니다. – Shri

관련 문제