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

时间:2012-02-27 15:19:03

标签: android listview android-edittext android-softkeyboard

我正在使用自定义基本适配器来实现customListView。 listView类(扩展ListView)在主要活动中的flipper flipper.addView(mListView)中使用。

列表视图有3种类型的行。 列表中的第1行是带有微调器的行,接下来的2行是带有edittext的行,其中输入了文本。第3行及以后的内容与包含数字的edittext相同。  我想这样做,以便当我点击文本版本时,软键盘将仅显示文本,而数字版本则显示电话键盘。
它们显示正常但问题出现在您单击编辑文本时,软键盘会以电话格式弹出。 它是在getView()中设置的所有设置和值,但是当以电话格式弹出软键盘时,getView()会再次被调用(逻辑)但是只要它触及文本类型EditTexts中的一个,键盘类型就会切换回文本输入。之后,它将无法轻易转回手机式显示屏。该视图似乎正在四处跳跃,并努力专注于我想要的EditText 我真的迷失在这里,无法弄清楚这一点。 以下是代码的两个主要部分。

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;

}

主视图

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

 }

}

xml,抱歉我无法正确插入。这是数字类型。

<?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>

2 个答案:

答案 0 :(得分:3)

经过几天和几天后,这是我的流畅可编辑列表视图的解决方案。 您必须设置2个绝对最重要的事项,以使其工作和可编辑而不会一直失去焦点。

最关键的1是。 在清单中

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

然后 在listview的xml中

android:descendantFocusability="beforeDescendants"

mListView.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

这最后一个只是在列表视图外的任何其他视图的顶部使用打开的编辑器放大列表视图,例如操作栏。适合小屏幕! 在此对话中找到stackoverflow link

答案 1 :(得分:1)

让我为listview中的edittext节省很多麻烦。不要这样做。我花了一个多星期的时间试图在listview中获取edittext以不回收输入,以确保当你恢复时正确的输入是在正确的字段中。我建议使用tablelayout创建一个滚动视图,该视图在一天结束时看起来像列表视图。只要你没有大量的行,这将是有效的。现在,如果我们说你有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));
            }}

希望这会有所帮助并为您省去很多麻烦。顺便说一下,我在xml(tablelayout,footer等)中构建了我可以通过java创建表格行。