0

我试图忽略输入键,但我不想使用 onKeyDown() 函数。

在 xml 中有一种方法可以做到这一点: 1. android:maxLines = "1" 2. android:lines = "1" 3. android:singleLine = "true"

我实际上想通过编码来完成最后一个。有谁知道这是怎么做到的吗?

for (int i=0; i<numClass; i++) {

        temp_ll = new LinearLayout(this);
        temp_ll.setOrientation(LinearLayout.HORIZONTAL);
        temp1 = new EditText(this);
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(12);
        temp1.setFilters(FilterArray);  // set edit text length to max 12
        temp1.setHint("  class name  ");
        temp1.setSingleLine(true);

        temp_ll.addView(temp1);
        frame.addView(temp_ll);
    }

    ll.addView(frame);
4

2 回答 2

1

这是我尝试过的:

public class Next extends Activity{
    LinearLayout lay;
    LinearLayout temp_ll;
    EditText temp1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);
    lay=(LinearLayout)findViewById(R.id.lay);



         temp_ll = new LinearLayout(this);
        temp_ll.setOrientation(LinearLayout.HORIZONTAL);
        temp1 = new EditText(this);
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(12);
        temp1.setFilters(FilterArray);  // set edit text length to max 12
        temp1.setHint("  class name  ");
        temp1.setSingleLine(true);

        temp_ll.addView(temp1);



    lay.addView(temp_ll);



}
}

xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>

</LinearLayout>
于 2012-06-22T08:17:46.347 回答
0

尝试使用setSingleLine

EditText editText=(EditText)findViewById(R.id.editv);

editText.setSingleLine(true);
于 2012-06-22T08:18:14.120 回答