2009-04-24 3 views
5

나는 다음과 같은 레이아웃을 사용하여 응용 프로그램이 있습니다왜 안드로이드 TabHost는 TextView에서 포커스를 훔치고 있습니까?

alt text http://img15.imageshack.us/img15/238/screenshot003xbo.png

응용 프로그램 시작 초점은 첫 번째 텍스트 뷰에 있지만 당신이 어떤 문자를 입력하려고하면, 초점이 직접 간다 탭. 내가 유일 fighting with this issue 아니라고 것, 그리고 어쩌면 이것은 다음과 관련이있다 : 어쨌든

http://groups.google.com/group/android-developers/browse_thread/thread/435791bbd6c550a/8022183887f38f4f?lnk=gst&q=tabs+focus#8022183887f38f4f

, 당신에게 그런 일이 왜 어떤 생각을 가지고? 그리고 물론, 모든 해결 방법은 인정 될 것입니다.

는 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"> 
    <LinearLayout 
      android:padding="5px" 
      android:orientation="vertical" 
      android:id="@+id/task_edit_panel" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="50" > 
     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/title" 
       android:textStyle="bold" /> 
      <EditText android:id="@+id/title" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
     </LinearLayout> 
     <TabHost android:id="@+id/edit_item_tab_host" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <TabWidget android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:id="@android:id/tabs" /> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:paddingTop="65px"> <!-- you need that if you don't want the tab content to overflow --> 
       <LinearLayout 
        android:id="@+id/edit_item_date_tab" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:padding="5px" > 
        <TextView android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="date" 
          android:textStyle="bold" /> 
       </LinearLayout> 
       <LinearLayout 
        android:id="@+id/edit_item_geocontext_tab" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:padding="5px" > 
       <TextView android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="lieu" 
          android:textStyle="bold" /> 
       </LinearLayout> 
       <LinearLayout 
        android:id="@+id/edit_item_text_tab" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:padding="5px"> 
       <EditText android:id="@+id/details" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:scrollbars="vertical" /> 
       </LinearLayout> 
      </FrameLayout> 
     </TabHost> 
    </LinearLayout> 
    <!-- Bottom pannel with "add item" button --> 
    <LinearLayout 
      android:padding="5px" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:id="@+id/task_edit_panel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#E7E7E7" > 
      <!-- Let the height set to fill_parent until we find a better way for the layout --> 
      <Button android:id="@+id/item_edit_ok_button" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:text="@string/ok" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_weight="1" /> 
      <Button android:id="@+id/item_edit_cancel_button" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:text="@string/cancel" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

그리고 자바 코드 : 그것은 관련

TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host); 
    // don't forget this setup before adding tabs from a tabhost using a xml view or you'll get an nullpointer exception 
    tab_host.setup(); 

    TabSpec ts1 = tab_host.newTabSpec("TAB_DATE"); 
    ts1.setIndicator(getString(R.string.when), getResources().getDrawable(R.drawable.ic_dialog_time)); 
    ts1.setContent(R.id.edit_item_date_tab); 
    tab_host.addTab(ts1); 

    TabSpec ts2 = tab_host.newTabSpec("TAB_GEO"); 
    ts2.setIndicator(getString(R.string.where), getResources().getDrawable(R.drawable.ic_dialog_map)); 
    ts2.setContent(R.id.edit_item_geocontext_tab); 
    tab_host.addTab(ts2); 

    TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT"); 
    ts3.setIndicator(getString(R.string.what), getResources().getDrawable(R.drawable.ic_menu_edit)); 
    ts3.setContent(R.id.edit_item_text_tab); 
    tab_host.addTab(ts3); 

    tab_host.setCurrentTab(0); 

답변

0

없는 경우


나는 질문 과부하를 방지하기 위해 아래의 코드를 게시 당신이 지적한 그 다른 버그 (그것은 아마 그 것처럼 보입니다.) 그리고 당신이 G1에 관해 이것을 경험하고 있다면 (그리고 에뮬레이터) 방향 변경 (키보드를 펼치고 가로 방향 모드로 전환 할 때) 전에 포커스가 고정되어 있지 않거나 뷰를 다시 만든 후 고정되지 않았다고 문제가 의심됩니다. 포커스를 저장/복원하는 데 activity의 onSaveInstanceState()onRestoreInstanceState() 메서드를 재정의 할 수 있습니다.

6

나는 HoneyComb과 같은 종류의 문제에 직면했지만 아주 간단한 해결책을 발견했다. 그래서 나는 누군가가 내 경험을 공유하는 데 도움이 될 수 있다고 생각했습니다. 탭을 클릭 할 때마다 텍스트 편집에 중점을두고 실제로 하드 키보드로 입력 할 수 있지만 소프트 키보드는 절대로 팝업되지 않습니다.

input1.requestFocusFromTouch(); 
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input1, InputMethodManager.SHOW_FORCED); 

해결책 : 당신이 탭을 처리하는 메인 클래스에서, 단지 자바 코드 아래 연락처 :

myTabHost.setOnTabChangedListener(new OnTabChangeListener(){  
    public void onTabChanged(String tabID) {  
     myTabHost.clearFocus(); 
    } 
}); 
0

이 예를 보여 어떻게 해결하는 방법도 아래의 코드로,이 문제를 해결 didnt는 이 문제 : 모든 사양을 추가 한 후

userNameEditText.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       userNameEditText.requestFocusFromTouch(); 
       return false; 
      } 
     }); 
0

자바 파일에 글고 치기로 requestFocus를 메소드를 추가

+1

당신은'EditText'에서 클래스를 파생시키고,'requestFocus' 메소드를 오버라이드하고 UI를 빌드 할 때 표준'EditText' 대신 그것을 사용한다는 것을 의미합니까? –

관련 문제