Android EditText不显示键盘

Android EditText不显示键盘,android,android-layout,Android,Android Layout,我有一个FragmentActivity,它加载一个RelativeLayout,在EditText和按钮上方有一个ListView。我似乎无法让键盘以任何方式显示出来。当我触摸它或设置焦点时,什么都没有发生。当我自动请求焦点时,什么也没有发生。如何让键盘显示出来?这不是应该是自动的吗?我想模拟GTALK的聊天输入 活动\u联系人\u chat.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:a

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

活动\u联系人\u 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);
    }

}
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.LoaderManager;
导入android.support.v4.content.CursorLoader;
导入android.support.v4.content.Loader;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类ContactChatFragment扩展片段实现LoaderManager.LoaderCallbacks{
ListView消息列表;
长联系电话号码;
字符串contact_UUID,contact_name;
私有静态最终int联系人\聊天\加载程序=0x11;
消息列表适配器;
私有静态最终字符串状态\u ACTIVATED\u POSITION=“ACTIVATED\u POSITION”;
private int mActivatedPosition=ListView.INVALID_POSITION;
公共静态最终字符串EXTRA\u CONTACTID=“item\u id”;
字符串联系人_rowID;
@凌驾
创建时的公共void(Bundle savedInstances){
super.onCreate(savedInstances);
if(getArguments().containsKey(额外的联系人ID)){
contact_rowID=getArguments().getString(额外的_CONTACTID);
//让我们联系一下
字符串投影[]={DBHelper.CONTACTS_ROWID,DBHelper.CONTACTS_UUID,DBHelper.CONTACTS_NAME,DBHelper.CONTACTS_PUBLIC_KEY};
游标contactCursor=getActivity().getContentResolver().query(Uri.withAppendedPath(ContactProvider.CONTENT\u Uri,String.valueOf(contact\u rowID)),投影,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=newmessagelistadapter(getActivity(),null,0);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
查看根视图=充气机。充气(R.layout.activity\u contact\u chat,container,false);
messageList=(ListView)rootView.findViewById(R.id.messages\u list);
messageList.setAdapter(适配器);
getLoaderManager();
/*
TextView empty=新的TextView(getActivity());
empty=(TextView)messageList.getEmptyView();
empty.setText(R.string.chat\u no\u消息);
empty.setVisibility(View.GONE);
((ViewGroup)messageList.getParent()).addView(空);
messageList.setEmptyView(空);
*/
EditText消息\输入\文本=(EditText)rootView.findViewById(R.id.chat\消息\输入);
message_input_text.clearFocus();
按钮消息\输入\按钮=(按钮)rootView.findViewById(R.id.details\记录\按钮);
message_input_button.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Toast.makeText(getActivity(),“嗨!”,Toast.LENGTH_SHORT.show();
}
});
返回rootView;
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
super.onSaveInstanceState(超出状态);
if(mActivatedPosition!=列表视图。位置无效){
//序列化并保留激活的项目位置。
outState.putInt(状态激活位置,mActivatedPosition);
}
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
String[]projection={DBHelper.MESSAGES\u ROWID,DBHelper.MESSAGES\u CONTENT,DBHelper.MESSAGES\u CONTENT\u LOCATION,DBHelper.MESSAGES\u READ\u DATE,DBHelper.MESSAGES\u UUID\u FROM,DBHelper.MESSAGES\u UUID\u TO};
CursorLoader CursorLoader=新的CursorLoader(getActivity(),MessageProvider.CONTENT\u URI,projection,null,null);
返回游标装入器;
}
@凌驾
public void onLoadFinished(加载器,光标){
适配器.swapCursor(游标);
}
@凌驾
公共void onLoaderReset(加载器){
适配器.swapCursor(空);
}
}

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

您可以向布局xml中的EditText添加以下代码:

android:focusable="true"
android:focusableInTouchMode="true"
另外,您可以在代码中将其设置为

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

移除

android:descendantFocusability="blocksDescendants" 

主要是相对论。您将在Edittext中获得焦点。BlockSubstands将阻止子布局获得焦点。检查

您是在模拟器中测试还是在设备中测试?两者都有。一个Droid RAZR、摩托罗拉XOOM和一个N7模拟器(512 MB)。所以键盘显示不是自动的?我不明白为什么这是“太本地化”。这样做了,2分钟后就可以接受了。我真不敢相信我错过了,太好了。你救了我的命。谢谢谢谢你救了我一天谢谢@Sahil。。你救了我一天:)谢谢分享它对我不起作用仍然看不到键盘小心显式设置android:inputType,否则键盘将无法打开。
android:descendantFocusability="blocksDescendants"