简体   繁体   English

EditText.SetText()在自定义适配器中更改我的软键盘输入类型

[英]EditText.SetText() changes my softkeyboard input type in a custom adapter

I am using a custom base adapter to implement a customListView. 我正在使用自定义基本适配器来实现customListView。 The listView class (extends ListView) is used within a flipper flipper.addView(mListView) in the main activity. listView类(扩展了ListView)在主活动中的脚蹼flipper.addView(mListView)中使用。

The list View has 3 types of rows. 列表视图具有3种类型的行。 The 1 st in the list is a row with spinner, the next 2 are rows with edittext where text is inputed. 列表中的第一个是带有微调器的行,接下来的两个是带有edittext的行,其中输入了文本。 The 3rd row and beyond are all the same with an edittext that has numbers in it. 第三行及以后的所有内容都相同,带有一个带有数字的edittext。 I wanted to make it so that when I click on the text version the softkeypad will appear with the text only and for the number version the phone keypad. 我想这样做,以便当我单击文本版本时,软键盘将只显示文本,而数字版本则显示电话键盘。
They display ok but the problem comes when you click on an edittext, the softkeyboard pops up fine in the phone format. 它们显示正常,但是当您单击编辑文本时出现问题,软键以电话格式弹出。 It is all setup and values set within getView() but when the softkeyboard pops up in the phone format getView() gets called again (logical) but as soon as it hits 1 of the text type EditTexts the keyboard type switches back to text input. 它是在getView()中设置和设置的所有值,但是当以电话格式弹出软键盘时,getView()会再次被调用(逻辑),但是一旦它到达文本类型EditTexts的1,键盘类型便会切换回文本输入。 It will cannot easily be turned back to a phone style display after that. 之后,将无法轻易将其返回到电话样式显示。 The view appears to be jumping around and struggling to focus on the EditText I want I am really lost here and can't figure this out. 该视图似乎在跳来跳去,努力将精力集中在EditText上,我希望我在这里真的迷失了自己,无法弄清楚。 Here are the 2 main bits of code. 这是代码的2个主要位。

public class MethodEditorAdapter extends BaseAdapter{

private Context context;
private ArrayList<String[]>  scanparam;

private LayoutInflater mInflater; 

public MethodEditorAdapter(Context context, ArrayList<String[]> scanparam ) { 
    super();
    this.scanparam = scanparam;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public long getItemId(int position) {
    int i = 0;
    if(position == 0) i = 0;
    if(position ==1) i = 1;         
    if(position == 2)i = 1;
    if (position > 2)i = 2;
    return i;
}
@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    int i = 0;
    if(position == 0) i = 0;
    if(position ==1) i = 1;         
    if(position == 2)i = 1;
    if (position > 2)i = 2;
    return i;
}

public View getView(int position, View convertView, ViewGroup parent) { 

    Formatnames(position);
    View rowView = convertView;
    ViewHolder holder = null;
    int type = getItemViewType(position);

    if (rowView == null ) {

        holder = new ViewHolder();
        switch (type) {
        case 0:
            rowView = mInflater.inflate(R.layout.method_editor_row_spin, null);
            holder.paramname = (TextView) rowView.findViewById(R.id.techniquetag);
            holder.techniquespinner = (Spinner) rowView.findViewById(R.id.techniquespinner);
            break;
        case 1:
            rowView = mInflater.inflate(R.layout.method_editor_row_text, null);
            holder.paramname = (TextView) rowView.findViewById(R.id.paramnameT);
            holder.paramvalue = (EditText) rowView.findViewById(R.id.paramvalT);
            break;
        case 2:
            rowView = mInflater.inflate(R.layout.method_editor_row_number, parent, false);
            holder.paramnameNum = (TextView) rowView.findViewById(R.id.paramnameN);
            holder.paramvalueNum = (EditText) rowView.findViewById(R.id.paramvalN);
            break;
        }
        rowView.setTag(holder);


    }else {
        holder = (ViewHolder) rowView.getTag();
    }

    setSelectedPosition(position);


    switch (type) {
    case 0:
        holder.paramname.setText(namestg + " " + nd);
        holder.techniquespinner.setSelection(Integer.valueOf(scanparam.get(position)[1]));
        break;
    case 1:
        holder.paramname.setText(namestg + " " + nd);
        holder.paramvalue.setText(scanparam.get(position)[1]);
        break;
    case 2:
    holder.paramnameNum.setText(namestg + " " + nd);
    holder.paramvalueNum.setText(scanparam.get(position)[1]);
    }

    return rowView;
}

static class ViewHolder {
    public TextView paramname;
    public EditText paramvalue;
    public Spinner techniquespinner;
    public TextView paramnameNum;
    public EditText paramvalueNum;

}

the main view 主要观点

public class MethodEditorView extends ListView {

private ArrayList<String[]> thismethod = new ArrayList<String[]>();

public MethodEditorAdapter editorAdapter;

private ListView mListView;

private Context mContext;

public MethodEditorView(Context context, ArrayList<String[]> methodlist) { 
    super(context);
    // TODO Auto-generated constructor stub
    this.thismethod = methodlist;
    mContext = context;enter code here
initview(context);
}

 private void initview(Context context){
    editorAdapter = new MethodEditorAdapter(context, thismethod );
    this.setAdapter(editorAdapter);

 }

} }

the xml, sorry I couldn't insert it properly. xml,抱歉,我无法正确插入。 this is for the number type. 这是用于数字类型的。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/methodrownumber">
    <TextView android:layout_height="wrap_content" android:layout_weight="1" android:textSize="16sp" android:textStyle="bold" android:id="@+id/paramnameN" android:layout_width="fill_parent" android:padding="5dp"></TextView>
    <EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="16sp" android:layout_weight="1" android:id="@+id/paramvalN" android:imeOptions="actionNext" android:inputType="phone" android:focusable="true" android:focusableInTouchMode="true" android:clickable="true"></EditText>

After days and days here's my solution for a smooth flowing editable listview. 几天又几天,这是我的解决方案,以实现流畅的可编辑列表视图。 There are 2 absolutely paramount things you have to set to make them work and editable without losing focus all the time. 您必须设置2个绝对重要的内容,以使它们能够正常工作和可编辑,而不会一直失去焦点。

The most critical 1 is. 最关键的是1。 in the manifest 在清单中

<activity android:name=".youractivity" android:windowSoftInputMode="adjustPan"/>

then in the xml of the listview 然后在列表视图的xml中

android:descendantFocusability="beforeDescendants"

or 要么

mListView.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

This last 1 simply enlarges the listview with an open editor over the top of any other views outside the listview such as an action bar. 最后一个1使用打开的编辑器简单地放大了列表视图,该编辑器位于列表视图之外的任何其他视图(例如操作栏)的顶部。 Nice for a small screen! 非常适合小屏幕! Found in the middle of this conversation stackoverflow link 在此对话的中间找到stackoverflow链接

Let me save you a lot of headache with the edittext in your listview. 让我用列表视图中的edittext省去很多麻烦。 Don't do it. 不要这样 I spent well over a week trying to get edittext in a listview to not recycle inputs, to make sure the right input is in the right field when you onresume. 我花了一个多星期的时间试图在列表视图中获取edittext,以免回收输入,以确保在继续时正确的输入位于正确的字段中。 I would recommend making a scroll view with a tablelayout that looks like a listview at the end of the day. 我建议在一天结束时制作一个带有类似于listview的表格布局的滚动视图。 As long as you don't have a huge number of rows this will work and run fine. 只要您没有大量的行,它将可以正常运行。 Now if lets say you have 100 rows, not gonna be pretty. 现在,如果说您有100行,那将不会很漂亮。

    public class inputpage extends Activity implements OnClickListener{

        public TableLayout tl;
        static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>();
        private dbadapter mydbhelper;
        public static int editCount;
        private PopupWindow pw;
        public Cursor cursor;
        private ArrayList<EditText> m_edit = new ArrayList<EditText>();

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mydbhelper = new dbadapter(this);
        mydbhelper.open();
        setContentView(R.layout.tablelayout);
        tl=(TableLayout) this.findViewById(R.id.table);
        getCursor();
        editCount = cursor.getCount();
        buildRow();

        View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);
        tl.addView(footer);





        }
//This lets me get a cursor so I can settext on my textviews
        public void getCursor(){
        if(main.quickStart == "Cate"){
            cursor = mydbhelper.getUserWord();
        }...

            //add my rows in a loop based off how many items my cursor brought back
        public void buildRow(){
            //params for my different items
            LayoutParams textparam = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, .30f);
            LayoutParams editparam = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, .70f);
            textparam.setMargins(2, 2, 2, 2);
            editparam.setMargins(2, 2, 2, 2);
            editparam.gravity=17;
            LayoutParams rowparam = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            cursor.moveToFirst(); //make sure cursor starts at beginning
            for (int i = 0; i < editCount; i++){
                TableRow tr = new TableRow(this);
                tr.setLayoutParams(rowparam); 

                //textview
                TextView tv=new TextView(this);
                tv.setLayoutParams(textparam);
                tv.setText(cursor.getString(cursor.getColumnIndex("userword")));
                tv.setTextSize(35f);
                tv.setGravity(Gravity.CENTER);
                tv.setWidth(175);
                tv.setTextColor(getResources().getColor(R.color.black));
                tr.addView(tv);

                //edittext
                EditText edit = new EditText(this);
                edit.setLayoutParams(editparam);
                tr.addView(edit);
                edit.setGravity(Gravity.LEFT);
                edit.setId(editCount);
                m_edit.add(i, edit);
                edit.setText("");

                cursor.moveToNext();
                tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            }}

Hope this helps and saves you a lot of trouble. 希望这会有所帮助并为您节省很多麻烦。 BTW I build what I can in xml (tablelayout, footer etc) and just make my table row via java. 顺便说一句,我建立了我可以在xml(tablelayout,footer等)中实现的功能,并且只是通过java使我的表行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM