1

是)我有的

我创建了一个表格布局,其中行是动态添加的。我有 2 个 editTexts 作为列。

我想要的是

  • 如果表格行的第二个 editText 获得焦点并按下回车键,则:
    1. 如果下面存在表格行,则应将焦点设置为其编辑文本。
    2. 如果下方不存在表格行,则应添加表格行,然后设置焦点。

我想我需要类似的东西:

If(tablerow exists below){
  //do the focusing
}
else{
  //add new row
  //do the focusing
}

我使用了一个函数addView来添加行:

public void addView(Context context) {

    int dpConv = (int) getResources().getDisplayMetrics().density;

    i = i + 1;
    try {

        txt_itemName[i] = new EditText(context);
        txt_itemName[i].setText("itName");
        txt_itemName[i].requestFocus();


        txt_qty[i] = new EditText(context);
        txt_qty[i].setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);
        txt_qty[i].setImeOptions(EditorInfo.IME_ACTION_NEXT);
        txt_qty[i].setOnEditorActionListener(new OnEditorActionListener() {

                public boolean onEditorAction(TextView v, int actionId, KeyEvent event)                                            

        {

                        if(actionId == EditorInfo.IME_ACTION_NEXT){

                                txt_qty[i].clearFocus();

                                //Need to check conditions here
                        }

        return false;
        }
        });

        tb_row[i] = new TableRow(context);
        tb_row[i].addView(txt_itemName[i]);
        tb_row[i].addView(txt_qty[i]);

        tb_lyt.addView(tb_row[i]);
4

1 回答 1

0

行。如果你试试这个怎么办:

if(actionId == EditorInfo.IME_ACTION_NEXT){

    txt_qty[i].clearFocus();

    //Need to check conditions here
    /* I'm assuming that:
          You have the value of i
          You add TableRows ONLY to your TableLayout
    */
    if(tb_lyt.getChildCount() > (i+1)) {
        //If a child exist below
        //Do your focus setting
    }
    else {
        //Add a new Row.
    }
 }
于 2012-08-30T06:19:49.477 回答