我有带有editext和textview的Listview。
当我触摸edittext然后edittext失去焦点!
我通过设置解决了这个问题android:windowSoftInputMode="adjustPan"(AndroidManifest.xml)
。现在我触摸edittext而不是editext获得焦点,但应用程序标签和一些原始列表视图消失(顶部)。
当用户触摸edittext而不丢失应用程序标签和一些原始列表视图时,我想获得焦点。
我已经实现的代码:
下面的代码在用户触摸edittext时获得焦点,但应用程序标签和一些原始列表视图在软键盘弹出时消失。我想在用户触摸edittext时获得焦点而不丢失应用程序标签和一些原始列表视图。
1)AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyListViewDemoActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
2) raw_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/mEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3) main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/mListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
4) MyListViewDemoActivity
public class MyListViewDemoActivity extends Activity {
private ListView mListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView=(ListView)findViewById(R.id.mListView);
mListView.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
private Activity mContext;
private String character[]={"a","b","c","d","e","f","g","h","i","j"};
public MyAdapter(Activity context)
{
mContext=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return character.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class Holder
{
EditText mEditText;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder;
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater =mContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.raw_layout, null);
holder.mEditText = (EditText) convertView
.findViewById(R.id.mEditText);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.mEditText.setText(character[position]);
holder.mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (!hasFocus){
final EditText etxt = (EditText) v;
holder.mEditText.setText(etxt.getText().toString());
}
}
});
return convertView;
}
}