安卓软键盘输入数字到文本编辑框中非常缓慢

5

我有一个TableLayout,其中包含多个产品。每行都包含代码、描述、数量、价格、折扣值等内容...根据用户输入的数量、折扣值、折扣数量和其他一些值也会进行计算。

当用户点击editText时,软键盘会出现,这也可以正常工作。

我的问题是,当用户按数字键时,EditText中显示得非常缓慢。

例如,我从键盘上按下3键,只有在7或8秒后它才显示在那个特定的EditText中。我该如何缩短这个时间线...

这是我的产品图片:

ProductImage

请有经验的人指导一下,为什么会发生这种情况?

代码类似于这样:

     for (int i = initil; i <end; i++) {
        .............
        ............
        final EditText txtQty = new EditText(this);
            txtQty.setHeight(1);
            txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 42));
            txtQty.setInputType(InputType.TYPE_CLASS_PHONE);
            txtQty.setImeOptions(EditorInfo.IME_ACTION_DONE);
            txtQty.setImeOptions(EditorInfo.IME_ACTION_NEXT);
            txtQty.setSelectAllOnFocus(true);
            txtQty.setTextSize(9);
            txtQty.setHint("0.0");
    //      txtQty.setOnEditorActionListener(new DoneOnEditorActionListener());
//          txtQty.setHighlightColor(R.color.green);
            tr.addView(txtQty); 

            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.showSoftInput(txtQty, InputMethodManager.SHOW_IMPLICIT);

            mgr.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_IMPLICIT_ONLY);
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(txtQty.getWindowToken(), 0);

            txtQty.setOnEditorActionListener( new OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    Log.i("KeyBoard" ,"Inside the Edit Text");
                    .............................
        } });

检查通过减少表格布局中的行数...... - viv
你有在真实设备上测试过吗? - Lalit Poptani
@Viv 目前第一页包含10条记录。我不能再减少了,因为有些客户有超过400条记录。所以,如果我们在循环外声明,是否可以在外部获取行的值?(我认为不行)。如果我们在外部声明,那就没问题了。 - Piraba
@LalitPoptani 我已经在真实设备上检查过了,结果也是一样的问题。这是因为监听器方法在循环内部。第一页(分页)包含10条记录。 - Piraba
1个回答

4

请检查此代码以获取动态表格布局:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#C0C0C0">

  <RelativeLayout
     android:layout_width="fill_parent" android:paddingBottom="20dip"
     android:layout_height="fill_parent"
     android:background="#C0C0C0">

   <TableLayout android:id="@+id/contact_table"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/contact_info_title"
      android:layout_marginTop="10dp"
      android:background="@drawable/bgwhite_selector">
      </TableLayout>
 </RelativeLayout>
</ScrollView>

要添加TableLayout的内容,请使用以下xml文件:
<?xml version="1.0" encoding="utf-8"?>
      <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/lays"
            android:layout_width="wrap_content" android:background="@color/white"
            android:layout_height="wrap_content" android:orientation="vertical"> 

<TableRow    android:background="@color/white" android:layout_width="wrap_content"
            android:layout_height="wrap_content">

<TextView   android:text=">" 
            android:textSize="18dip" android:textStyle="bold"  android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:id="@+id/arrowText"/> 
  </TableRow>

 </LinearLayout>

在你为布局创建单独的行之后,在Java代码中添加以下内容:
contact_table = (TableLayout)findViewById(R.id.contact_table);

LayoutInflater inflater = getLayoutInflater();

for(int i = 0; i < contact_count ; i++) {
LinearLayout row = (LinearLayout)inflater.inflate(R.layout.table_row,contact_table, false);
TextView text = (TextView)row.findViewById(R.id.text);
text.setText(list_data.get(i).summary);
contact_table.addView(row);
  }

 for(int i=0;i<contact_table.getChildCount();i++){ 
final View row=contact_table.getChildAt(i);
row.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        // TODO Auto-generated method stub
        row_id=contact_table.indexOfChild(row);
    }
});
}

第二个循环是获取动态创建的表格行的点击事件,在其中添加以下内容:
    msg_title_text.setOnEditorActionListener(new DoneOnEditorActionListener());

对应的动作监听器:

    class DoneOnEditorActionListener implements OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Log.v("*****************************", "Clicked");

            return true;    
        }
        return false;
    }
}

有一些缓慢,但与我的以前的代码相比,这减少了时间。谢谢@Venky - Piraba
@Venky,我的表格中有3列,我的疑问是如何使用这种方法拉伸我的第二列? - Victor Laerte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接