检查表格布局中是否存在表格行

时间:2012-08-30 05:29:32

标签: android android-layout android-view android-tablelayout

我有什么

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

我想要什么

  • 如果表格行的第二个editText被聚焦并按下了输入,则:
    1. 如果下面存在表格行,则应将焦点设置为它的edittext。
    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]);

1 个答案:

答案 0 :(得分: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.
    }
 }