Android:为什么我的EditText字段失去焦点?

时间:2012-03-25 09:22:08

标签: android listview focus android-edittext

我已经多次看到这个问题了,我认为这并不难解决;我还是试了好几个小时,不知道它是如何运作的。

简单问题:我在listView中有2个EditText,当我触摸它们的字段时,它们都会立即松散焦点。

必须是一些层次结构的东西。实际上我可以用adjustpan属性修复它,但是当softKeyboard打开时我的listView不再滚动。

这是我的代码:

package com.android.activity;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class NotesCursorAdapter extends CursorAdapter{

    private Context context;
    //  private Cursor cursor;
    private int addNoteTopPosition;
    private int addNoteBottomPosition;
    private LayoutInflater inflater;
    private boolean deleteMode = false;

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_ADD_NOTE_BOTTOM = 1;
    private static final int TYPE_ADD_NOTE_TOP = 2;
    private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1;

    public NotesCursorAdapter (Context context, Cursor cursor, int flag, boolean enableDelete){
        super(context, cursor);
        this.context = context;
        addNoteTopPosition = 0;
        addNoteBottomPosition = cursor.getCount()+1;

        inflater = LayoutInflater.from(this.context);

        deleteMode = enableDelete;

    }

    @Override
    public int getCount() {  
        return super.getCount() + 2;
    } 

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {

            case TYPE_ADD_NOTE_TOP:
                convertView = inflater.inflate(R.layout.add_note_top, null);
                holder.view = (EditText)convertView.findViewById(R.id.add_note_top_id);
                break;
            case TYPE_ITEM:
                convertView = inflater.inflate(R.layout.row_note, null);
                holder.delete = (Button)convertView.findViewById(R.id.delete);
                if (deleteMode){
                    holder.delete.setVisibility(View.VISIBLE);
                }else{
                    holder.delete.setVisibility(View.GONE);
                }
                holder.delete.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    }
                });

                holder.view = (TextView)convertView.findViewById(R.id.note);
                getCursor().moveToPosition(position - 1);
                ((TextView) holder.view).setText(getCursor().getString(getCursor().getColumnIndex("content_note")));
                break;
            case TYPE_ADD_NOTE_BOTTOM:
                convertView = inflater.inflate(R.layout.add_note_bottom, null);
                holder.view = (EditText)convertView.findViewById(R.id.add_note_bottom_id);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        int type;
        if (position == addNoteTopPosition){
            type = TYPE_ADD_NOTE_TOP;
        } else if (position == addNoteBottomPosition){
            type = TYPE_ADD_NOTE_BOTTOM;
        }else {
            type = TYPE_ITEM;
        }
        return type;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public long getItemId(int position) {  
        return position;  
    }

    public static class ViewHolder {
        public View delete;
        public View view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return null;
    }

}

以下是XML文件:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_diary_big"
    android:focusable="false"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_gravity="right"
        android:gravity="fill_vertical"
        android:orientation="horizontal"
        android:paddingRight="10dp"
        android:paddingTop="10dp" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <Button
                android:id="@+id/calendar"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right"
                android:background="@drawable/calendar_up" />
        </LinearLayout>

        <TextView
            android:id="@+id/currentDay"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="0.5"
            android:gravity="center_vertical|center_horizontal"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical" >

            <Button
                android:id="@+id/done"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/pen_blue_big" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:orientation="vertical" >

        <ListView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:cacheColorHint="#00000000"
            android:descendantFocusability="beforeDescendants" >
        </ListView>
    </LinearLayout>

</LinearLayout>

add_note_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/add_note_top_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:hint="@string/add_note"
    android:textColor="#000000" >

</EditText>

add_note_bottom.xml:

  <EditText
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/add_note_bottom_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/add_note"
        android:background="#00000000"
        android:textColor="#000000" >
    </EditText>

row_note.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutContainer"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_gravity="right"
    android:gravity="fill_vertical"
    android:orientation="horizontal"
    android:paddingRight="10dp"
    android:paddingTop="10dp" >

    <Button
        android:id="@+id/delete"
        android:layout_width="21dp"
        android:layout_height="22dp"
        android:background="@drawable/sens_interdit" />

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/note"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:focusable="false"
        android:textColor="#000000" >
    </TextView>

</LinearLayout>

我认为我认为重要的是,如果你们需要一些更像活动代码或其他东西,请问。

感谢阅读。

1 个答案:

答案 0 :(得分:0)

尝试设置

android:windowSoftInputMode="adjustPan"

表示您在AndroidManifest.xml中的活动