23

我有一个 FragmentActivity,它在 EditText 和一个按钮上方加载一个带有 ListView 的 RelativeLayout。我似乎无法以任何方式显示键盘。当我触摸它或将焦点放在它上面时,什么也没有发生。当我自动请求焦点时,什么也没有发生。如何让键盘显示?这不应该是自动的吗?我想模拟 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>

联系聊天片段:

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

}
4

2 回答 2

40

只需删除

android:descendantFocusability="blocksDescendants" 

在主要的相对布局中。您将在 Edittext.BlockDescendants 中获得焦点,这将阻止子布局获得焦点。检查http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

于 2012-12-31T06:08:02.597 回答
34

当 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);
于 2012-12-31T06:07:53.120 回答