問題描述
我有帶有 editext 和 textview 的 Listview.
I have Listview with editext and textview.
當我觸摸 edittext 時,edittext 失去焦點!
When i touch on edittext then edittext lost focus!
我通過設置 android:windowSoftInputMode="adjustPan"(AndroidManifest.xml)
解決了這個問題.現在我觸摸edittext而不是editext獲得焦點,但應用程序標簽和一些原始列表視圖消失(頂部).
I resolved this problem by setting android:windowSoftInputMode="adjustPan"(AndroidManifest.xml)
.
Now i touch on edittext than editext get focus but application label and some raw of listview disappear(top part).
我想在用戶觸摸 edittext 時獲得焦點,而不會丟失應用程序標簽和一些原始列表視圖.
I want to get focus when user touch on edittext without loss application label and some raw of listview.
我已經實現的代碼:
當用戶觸摸edittext但應用程序標簽和一些原始列表視圖在軟鍵盤彈出時消失時,下面的代碼獲得焦點.我想在用戶觸摸edittext時獲得焦點而不丟失應用程序標簽和一些原始列表視圖.
Below coding get focus when user touch on edittext but application label and some raw of listview disappear when soft keypad pop up.I want to get focus when user touch on edittext without loss application label and some raw of listview.
1)AndroidManifest.xml
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
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
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
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 失去焦點.
I was having the same problem. My numberic keyboard would momentarily appear before being replaced by the qwerty keyboard and the EditText losing focus.
問題是出現的鍵盤使您的 EditText 失去焦點.為防止這種情況發生,請在您的 AndroidManifest.xml 中為適當的活動(或活動)添加以下內容:
The problem is that the keyboard appearing makes your EditText lose focus. To prevent this put the following in your AndroidManifest.xml for the appropriate Activity (or Activities):
android:windowSoftInputMode="adjustPan"
請參閱 Android 文檔:
See Android documentation:
當輸入法出現在屏幕上時,它會減少可用于應用 UI 的空間量.系統會決定如何調整 UI 的可見部分,但它可能無法正確處理.為確保您的應用獲得最佳行為,您應該指定您希望系統如何在剩余空間中顯示您的 UI.
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.
要在活動中聲明您的首選處理方式,請在清單的 <activity>
元素中使用 android:windowSoftInputMode
屬性和adjust"之一.價值觀.
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.
例如,為了確保系統將您的布局調整到可用空間——這確保所有布局內容都可以訪問(即使它可能需要滾動)——使用 adjustResize"
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"
這篇關于Listview android中的Edittext的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!