【问题标题】:How to take input from the middle of any sentence programmatically in java?如何在java中以编程方式从任何句子的中间获取输入?
【发布时间】:2016-12-05 05:31:20
【问题描述】:

我正在开发一个类似于填补空白的应用程序。我想从差距中获取信息。此代码将自动测量间隙的位置。

 String Str = new String(text);
    System.out.print("Found Index :");
    System.out.println(Str.indexOf('_'));

    Log.e("Index", String.valueOf(Str.indexOf('_')));


    Rect bounds = new Rect();
    Paint textPaint = questionView.getPaint();
    textPaint.getTextBounds(text, 0, Str.indexOf('_'), bounds);
    int height = bounds.height();
    int firstWidth = bounds.width();
    textPaint.getTextBounds(text, Str.indexOf('_') + 3, text.length(), bounds);
    int secondWidth = bounds.width();


    Log.e("String Bounds", "firstWidth " + firstWidth + " secondWidth " + secondWidth);


    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(firstWidth, 0, secondWidth, 0);
    answerText.setLayoutParams(lp);

这段代码的输出类似这样:

现在问题出现了。如何设置用户可以输入的editText的长度以填补空白?

我想包括整个句子是“这是一个句子”。该句子将像“这是___句子”一样出现在用户面前。如果用户触摸间隙,我想接受输入。我们也可以放置一个像“Enter”这样的按钮来接受输入。如何正确地做到这一点,因为现在我可以测量句子的宽度和高度。

【问题讨论】:

  • 想要更多解释

标签: java android xml android-edittext


【解决方案1】:

您好,您可以实现 TextEditMask 或使用此库

Masked-Edittext

使用:

MaskedEditText maskedEditText = new MaskedEditText(this,
                "This is ___ sentence",
                "_",
                getResources().getDrawable(R.drawable.ic_account_circle),
                new MaskedEditText.MaskIconCallback() {
                    @Override
                    public void onIconPushed() {
                        System.out.println("Icon pushed");
                        //Invoke here contact list or just clear input
                    }
                });

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多