2012-08-30 71 views
0

我打算以顯示與在它一個EditText一個彈出窗口,並顯示一次鍵盤可以自動激活:EditText上沒有鍵盤在橫向模式下

public void showPopup(View view) // a button invoke 
{ 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View content = inflater.inflate(R.layout.popup, null); 
    PopupWindow pw = new PopupWindow(content, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); 
    pw.setBackgroundDrawable(new BitmapDrawable()); 
    pw.showAtLocation(view, Gravity.TOP, 0, 0); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    (new Handler()).post(new Runnable() { 
     public void run() { 
      InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 
     } 
    }); 
} 

和popup.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

> 
<EditText android:id="@+id/edit_message" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:text="Hello Android!!" 
android:hint="Filling in it!!" 
android:layout_weight="1" 
android:selectAllOnFocus="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
/> 
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Go!" 

/> 
</LinearLayout> 

它適用於縱向模式,但在添加代碼後不會顯示在橫向模式下:

protected void onResume() { 
    if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 
} 

可以請求任何人提供一些解決方案嗎?

回答

0

在EditText上requestFocus的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

> 
<EditText android:id="@+id/edit_message" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:text="Hello Android!!" 
android:hint="Filling in it!!" 
android:layout_weight="1" 
android:selectAllOnFocus="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
><requestFocus/></EditText> 
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Go!" 

/> 
</LinearLayout> 
+0

我進行了嘗試,但還是鍵盤沒有激活.. – lindeer

1

編輯添加在XML中的popup.xml文件

<LinearLayout............ 
    ..... 
    android:focusable="true" 
android:focusableInTouchMode="true" ... 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:focusable="true" 
android:focusableInTouchMode="true" 
> 
<EditText android:id="@+id/edit_message" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:text="Hello Android!!" 
android:hint="Filling in it!!" 
android:layout_weight="1" 
android:selectAllOnFocus="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
/> 
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Go!" 
/> 
</LinearLayout> 
+0

這會讓EditText失去焦點... – lindeer

+0

與popup.xml文件相同,但只添加(android:focusable =「true」和android:focus ableInTouchMode =「true」)在LinearLayout中... –

相關問題