2011-05-24 47 views
1

我希望在我的Activity啓動時打開鍵盤。我已經嘗試過所有的方法/答案,下面的問題沒有運氣。活動開始時軟鍵盤不顯示 - 更新?

我相信問題是當硬件鍵盤可用時,默認行爲是軟鍵盤不能顯示。這可以被覆蓋嗎?如果硬件鍵盤被隱藏會發生什麼?

我讀過以下問題,沒有運氣。最接近我遇到的問題是在這裏: Question 2712822

其他還包括:
Question 3379403
Question 2479504

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical"> 
     <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent"> 
      <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView> 
      <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/testText" android:focusable="true" android:focusableInTouchMode="true" android:hint="Input here"></EditText> 
     </LinearLayout> 
     <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent"> 
      <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView> 
      <EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="and here"></EditText> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

我的主要活動代碼如下所示:

package com.example.example3; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.EditText; 

public class example3 extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    EditText edit = (EditText)this.findViewById(R.id.testText); 
    edit.requestFocus(); 

    /*  // Below Doesn't work 
    InputMethodManager imm = (InputMethodManager) 
    example3.this.getSystemService(Context.INPUT_METHOD_SERVICE); 

    if (imm != null){/   imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
    } 

    //Below Doesn't work 
    // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    */  
    } 
} 

這是一個失敗的原因嗎?有人可以用硬件鍵盤關閉的手機進行測試,並告訴我會發生什麼?

回答

0

softinputMode相關的答案應該工作。如果您在清單中設置它沒有運氣,您可以嘗試在onCreate()中設置它以指示在導航到活動時如何顯示鍵盤。

要隨時顯示它的活動獲得焦點的使用:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
+0

我已經在我的代碼中嘗試了這一點......目前它已被註釋掉。這並不成功。你知道仿真器/真實設備當前是否打開硬件鍵盤,是否會覆蓋任何通過編程打開鍵盤的嘗試? – hydrox467 2011-05-30 03:41:22

+0

當硬件鍵盤暴露時,軟件鍵盤不應該打開 – jqpubliq 2011-06-01 17:59:08

0

這definitley工作,我一直使用它,不要從onCreate調用它,從onStart或onResume調用它。

public static void showKeyboard(Activity act,EditText t){ 
      InputMethodManager imm = (InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.showSoftInput(t, 0); 
} 
+0

除了我上面的評論,在測試這種方法時,您是否專門設置了啓用觸摸的模擬器,並禁用了硬件鍵盤以使其工作?我已經在我的機器人公司上試過了,並且成功了,但沒有使用模擬器。 – hydrox467 2011-05-30 03:42:53

0

試試這個

edtReceiver.setOnFocusChangeListener(new OnFocusChangeListener() { 

       @Override 
       public void onFocusChange(View v, boolean hasFocus) { 
        if(hasFocus) 
        { 

       // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
         if(imm != null) 
         { 
         imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
         } 
        } 

       } 
      }); 

這對我的作品。