gpt4 book ai didi

android - Listview android中的Edittext

转载 作者:IT王子 更新时间:2023-10-28 23:32:01 25 4
gpt4 key购买 nike

我有带有 editext 和 textview 的 Listview。

当我触摸 edittext 时,edittext 失去焦点!

我通过设置 android:windowSoftInputMode="adjustPan"(AndroidManifest.xml) 解决了这个问题。现在我触摸edittext而不是editext获得焦点,但应用程序标签和一些原始 ListView 消失(顶部)。

我想在用户触摸 edittext 时获得焦点,而不会丢失应用程序标签和一些原始 ListView 。

我已经实现的代码:

当用户触摸edittext但应用程序标签和一些原始 ListView 在软键盘弹出时消失时,下面的代码获得焦点。我想在用户触摸edittext时获得焦点而不丢失应用程序标签和一些原始 ListView 。

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

}

最佳答案

我遇到了同样的问题。我的数字键盘会在被 qwerty 键盘替换之前暂时出现,并且 EditText 失去焦点。

问题是出现的键盘使您的 EditText 失去焦点。为防止这种情况发生,请在您的 AndroidManifest.xml 中为适当的 Activity (或 Activity )添加以下内容:

android:windowSoftInputMode="adjustPan"

参见 Android documentation :

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's <activity> element with one of the "adjust" values.

For example, to ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)—use "adjustResize"

关于android - Listview android中的Edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com