Android开发:方向更改后的软键盘问题

时间:2014-11-18 21:56:45

标签: android keyboard orientation show

我有一个由ListView组成的片段。此ListView的每个单元格都包含一个EditView。

让我们假装在我输入文字时,我决定轮换我的设备。引入了问题:

1)键盘关闭。我想在旋转设备后保持键盘可见。 2)更糟糕的是,附加到每个EditText对象的任何侦听器都不再处于活动状态。虽然键盘可能会开始出现,但是我没有修改那个聚焦的EditText里面的文字,因为我正在打字。

我该如何处理这些问题?

源代码: 片段...

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment, container, false);

    mListView = (ListView)mView.findViewById(R.id.listView);

    prepareListData();

    mAdapter = new ThermostatExtrasListAdapter(mContext, mThermostat, listItems);

    mListView.setAdapter(mAdapter);

    prepareListViewListeners();

    return mView;
}

private void prepareListData() {
    listItems = new ArrayList<Object>();

    for (Object nextObject : mObjects) {
            listItems.add(nextObject);
    }
}

...适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    DataStructure obj = (DataStructure)this.getItem(position);

    LayoutInflater inflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    convertView = inflater.inflate(R.layout.list_item_textfield, parent, false);

    if(convertView != null) {
        final EditText valueObject = (EditText) convertView.findViewById(R.id.textEdit);
        valueObject.setText(obj.value);
        valueObject.setTag(obj);

        valueObject.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                extraObject.value = s.toString();
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        valueObject.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    //TODO: Do something...
                }
                return false;
            }
        });
    }

    return convertView;
}

1 个答案:

答案 0 :(得分:1)

1)在清单中添加以下行:  机器人:windowSoftInputMode =&#34; stateUnchanged&#34; 2)您不应为每个EditText创建一个监听器。 这段代码应该有效:

private OnEditorActionListener mOnEditorActionListener = ...;
@Override
public View getView(int position, View convertView, ViewGroup parent) {

        ...
        valueObject.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                extraObject.value = s.toString();
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        valueObject.setOnEditorActionListener(mOnEditorActionListener);
        ...

}
相关问题