EditText不显示键盘

我有一个FragmentActivity,加载一个带有一个ListView上方的EditText和一个button的RelativeLayout。 我似乎无法让键盘出现在任何方法。 当我触摸它或设置焦点时,什么都不会发生。 当我自动请求焦点时,没有任何反应。 我如何让键盘显示出来? 这不应该是自动的吗? 我想模拟GTALK的聊天input。

activity_contact_chat.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/contact_detail_container" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <Button android:id="@+id/details_record_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignTop="@+id/chat_message_input" android:text="Send" /> <EditText android:id="@+id/chat_message_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/details_record_button" android:ems="10" android:gravity="top|left" android:hint="@string/message_hint" android:lines="1" android:maxLines="10" android:paddingRight="10dp" android:paddingTop="10dp" android:scrollbars="vertical" > </EditText> <ListView android:id="@+id/messages_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/chat_message_input" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#000000" > </ListView> </RelativeLayout> 

ContactChatFragment:

 import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ContactChatFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{ ListView messageList; Long contact_id; String contact_UUID, contact_name; private static final int CONTACT_CHAT_LOADER = 0x11; MessageListAdapter adapter; private static final String STATE_ACTIVATED_POSITION = "activated_position"; private int mActivatedPosition = ListView.INVALID_POSITION; public static final String EXTRA_CONTACTID = "item_id"; String contact_rowID; @Override public void onCreate(Bundle savedInstances) { super.onCreate(savedInstances); if (getArguments().containsKey(EXTRA_CONTACTID)) { contact_rowID = getArguments().getString(EXTRA_CONTACTID); //Let's get the contact String projection[] = { DBHelper.CONTACTS_ROWID, DBHelper.CONTACTS_UUID, DBHelper.CONTACTS_NAME, DBHelper.CONTACTS_PUBLIC_KEY }; Cursor contactCursor = getActivity().getContentResolver().query(Uri.withAppendedPath(ContactProvider.CONTENT_URI, String.valueOf(contact_rowID)),projection, null, null, null); if (contactCursor.moveToFirst()) { contact_id = contactCursor.getLong(contactCursor.getColumnIndex(DBHelper.CONTACTS_ROWID)); contact_UUID = contactCursor.getString(contactCursor.getColumnIndex(DBHelper.CONTACTS_UUID)); contact_name = contactCursor.getString(contactCursor.getColumnIndex(DBHelper.CONTACTS_NAME)); } } adapter = new MessageListAdapter(getActivity(), null, 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_contact_chat, container, false); messageList = (ListView) rootView.findViewById(R.id.messages_list); messageList.setAdapter(adapter); getLoaderManager().initLoader(CONTACT_CHAT_LOADER, null, this); /* TextView empty = new TextView(getActivity()); empty = (TextView) messageList.getEmptyView(); empty.setText(R.string.chat_no_messages); empty.setVisibility(View.GONE); ((ViewGroup) messageList.getParent()).addView(empty); messageList.setEmptyView(empty); */ EditText message_input_text = (EditText) rootView.findViewById(R.id.chat_message_input); message_input_text.clearFocus(); Button message_input_button = (Button) rootView.findViewById(R.id.details_record_button); message_input_button.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getActivity(), "Hi!",Toast.LENGTH_SHORT).show(); } }); return rootView; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mActivatedPosition != ListView.INVALID_POSITION) { // Serialize and persist the activated item position. outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); } } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] projection = {DBHelper.MESSAGES_ROWID, DBHelper.MESSAGES_CONTENT, DBHelper.MESSAGES_CONTENT_LOCATION, DBHelper.MESSAGES_LOCATION, DBHelper.MESSAGES_READ_DATE, DBHelper.MESSAGES_UUID_FROM, DBHelper.MESSAGES_UUID_TO}; CursorLoader cursorLoader = new CursorLoader(getActivity(), MessageProvider.CONTENT_URI, projection, null, null, null); return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } } 

Solutions Collecting From Web of "EditText不显示键盘"

只要删除

 android:descendantFocusability="blocksDescendants" 

在主RelativeLayout中。 您将在Edittext中获得焦点.BlockDesndndants将防止子布局获得焦点。 检查http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

当活动获得焦点时,您可以使用任一种方法来显示软键盘,

您可以将以下代码添加到Layout xml中的EditText,

 android:focusable="true" android:focusableInTouchMode="true" 

另外,你可以在代码中设置它,

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

要么,

  InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.showSoftInput(edittext, 0);