为什么android:inputType =“textMultiLine | textEmailAddress”允许多行

时间:2013-09-21 15:41:02

标签: android android-edittext

我有一个EditText输入字段,需要接受多个电子邮件地址 例如多行。
但是设置android:inputType="textMultiLine|textEmailAddress"
textEmailAddress选项会停止输入多行 我搜索过google和SO,并且所有“解决方案”都没有解决这个问题 什么是有效的解决方案?

5 个答案:

答案 0 :(得分:1)

试试这个......这对我有用

  1. android:singleLine =“false”
  2. android:lines =“10”// max lines
  3. android:minLines =“3”//这将是身高
  4. 看看这个EditText

      <EditText
        android:id="@+id/addr_edittext"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:gravity="top|left"
        android:inputType="textEmailAddress|textMultiLine"
        android:lines="20"
        android:minLines="5"
        android:scrollHorizontally="false"
        android:scrollbars="vertical"
        android:singleLine="false" />
    

答案 1 :(得分:1)

  

但是通过设置android:inputType =“textMultiLine | textEmailAddress”   textEmailAddress选项会停止输入多行。

textEmailAddresstextMultiline是IME(输入法编辑器)的提示。 Android设备上有许多输入法编辑器,并非所有人都必须按照您想要的方式来表达。 textEmailAddresstextMultiline无法在某些IME上合作,我感到非常惊讶。

以下是一些选项:

  1. 只需使用textMultiline并希望用户不会用干草叉攻击你,因为他们很难找到 @ 键。

  2. 使用多个EditText小部件。例如,您可能有一个(或几个)textEmailAddress EditText个小部件以及+ ImageButton来添加更多内容,每个EditText收集一个地址

答案 2 :(得分:0)

您是否为EditText设置了MaxLine attribute

试试这段代码:

  <EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="fill_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
    android:singleLine="false" <!--Enables insertion of new line with "enter" -->
  />

答案 3 :(得分:0)

如果您要添加多个收件人,例如Gmail或任何电子邮件......

请通过类似的帖子

<强> Android EditText Gmail like to field

或者您可以制作多封电子邮件自动填充TextView。

public class ContactsAutoComplete extends AutoCompleteTextView {

    public ContactsAutoComplete(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
        this.setThreshold(0);
        this.setUpContacts();
    }

    public ContactsAutoComplete(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        this.setThreshold(0);
        this.setUpContacts();
    }

    public ContactsAutoComplete(final Context context) {
        super(context);
        this.setThreshold(0);
        this.setUpContacts();
    }

    // --- comma separating stuff

    private String previous = ""; //$NON-NLS-1$
    private String seperator = ", "; //$NON-NLS-1$

    /**
     * This method filters out the existing text till the separator and launched
     * the filtering process again
     */
    @Override
    protected void performFiltering(final CharSequence text, final int keyCode) {
        String filterText = text.toString().trim();
        previous = filterText.substring(0,
                filterText.lastIndexOf(getSeperator()) + 1);
        filterText = filterText.substring(filterText.lastIndexOf(getSeperator()) + 1);
        if (!TextUtils.isEmpty(filterText)) {
            super.performFiltering(filterText, keyCode);
        }
    }

    /**
     * After a selection, capture the new value and append to the existing text
     */
    @Override
    protected void replaceText(final CharSequence text) {
        super.replaceText(previous + text + getSeperator());
    }

    public String getSeperator() {
        return seperator;
    }

    public void setSeperator(final String seperator) {
        this.seperator = seperator;
    }

    // --- contacts stuff

    private void setUpContacts() {
        ContactListAdapter adapter = new ContactListAdapter(getContext(), null);
        setAdapter(adapter);
    }

    @SuppressWarnings("nls")
    public static class ContactListAdapter extends CursorAdapter implements Filterable {
        public ContactListAdapter(Context context, Cursor c) {
            super(context, c);
            mContent = context.getContentResolver();
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            final LayoutInflater inflater = LayoutInflater.from(context);
            final TextView view = (TextView) inflater.inflate(
                    android.R.layout.simple_dropdown_item_1line, parent, false);
            view.setText(convertToString(getCursor()));
            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            ((TextView) view).setText(convertToString(cursor));
        }

        @Override
        public String convertToString(Cursor cursor) {
            return cursor.getString(1) + " <" + cursor.getString(2) +">";
        }

        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) {
                return getFilterQueryProvider().runQuery(constraint);
            }

            StringBuilder buffer = null;
            String[] args = null;
            if (constraint != null) {
                constraint = constraint.toString().trim();
                buffer = new StringBuilder();
                buffer.append("UPPER(").append(People.NAME).append(") GLOB ?");
                buffer.append(" OR ");
                buffer.append("UPPER(").append(ContactMethods.DATA).append(") GLOB ?");
                args = new String[] { constraint.toString().toUpperCase() + "*",
                        constraint.toString().toUpperCase() + "*" };
            }

            return mContent.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,
                    PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(), args,
                    Contacts.People.DEFAULT_SORT_ORDER);
        }

        private final ContentResolver mContent;
    }

    private static final String[] PEOPLE_PROJECTION = new String[] {
        People._ID,
        People.NAME,
        ContactMethods.DATA
    };
}

使用此自定义AutoCompleteTextView,如下所示

<YOUR_PACKAGE.ContactsAutoComplete
        android:id="@+id/emails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Send To"
        android:scrollbars="vertical"
        android:singleLine="false" />

添加此权限

<uses-permission android:name="android.permission.READ_CONTACTS" />

请参阅以下博客。

http://www.betaful.com/2011/02/multiple-e-mail-autocomplete-in-android/

希望这会对你有所帮助。

答案 4 :(得分:-1)

android:inputType="textMultiLine|textEmailAddress"

将单行设为false。这将允许编辑文本中的多行。