EditText没有显示键盘

时间:2012-12-31 05:56:50

标签: android android-layout

我有一个FragmentActivity,它在EditText和Button上面加载了一个ListL,以及ListView。我似乎无法让键盘显示在任何方法中。当我触摸它或设置焦点时,没有任何反应。当我自动请求焦点时,没有任何反应。如何让键盘出现?这不应该是自动的吗?我想模拟GTALK的聊天输入。

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);
    }

}

2 个答案:

答案 0 :(得分:28)

只需删除

android:descendantFocusability="blocksDescendants" 
主要的RelativeLayout中的

。您将获得Edittext.BlockDescendants的焦点将阻止子布局获得焦点。查看http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

答案 1 :(得分:20)

当Activity获得焦点时,您可以使用其中一种方法显示软键盘

您可以将以下代码添加到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);