如何在 edittext 上创建特定的输入类型?输入可接受的是罗马数字 M D C X V I

how can create specific input type on edittext? The input accettable is Roman numbers M D C X V I

我已经扩展了 edittext class,如何创建自定义输入类型来接受这些字符 (M、C、D、X、V、I)?这是我扩展 EditText

的 class
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;

public class DecimalEditText extends EditText{

    public DecimalEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub


    }
    public int getArabic(String roman){
        ConversioneTest ca = new ConversioneTest();
        int i = ca.convertToDecimal(roman);
        return i;

    }

}

例如,您需要实施 LoginFilter.UsernameFilterGenericInputFilter to restrict the input to the acceptable characters and assign that to the EditText. In first iteration, this is really simple. Take a look at the source code。您将在几分钟内完成设置。

如果这就是您所需要的,太好了,到此为止!不幸的是,这可能就是您所需要的。罗马数字有一些语法规则,比如XXCI是(100-20)+1=81,但是IXXC不是100-(20-1)=81。如果你想覆盖它,那么 InputFilter 就太粗糙了。您仍将使用 InputFilter 进行初始过滤,但您必须实施 TextWatcher on top of that to handle the advanced input validation. The PhoneNumberFormattingTextWatcher (source) 为您提供一些有关如何执行此操作的线索。这比 InputFilter 的工作要多得多,因为您必须处理各种奇怪的多字符输入,例如剪贴板粘贴到替换选择。