如何通过编码(忽略输入)将EditText行限制为1?

时间:2012-06-22 07:59:53

标签: android android-edittext

我试图忽略回车键,但我不想使用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);

2 个答案:

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

答案 1 :(得分:0)

尝试使用setSingleLine作为:

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

editText.setSingleLine(true);