0

singlelineEditTextがあり、コンテンツのサイズと のサイズに興味があり、コンテンツが のサイズにEditText適合するかどうかを確認しますEditText。これどうやってするの?

ありがとう

4

1 に答える 1

2

あなたがここで何を求めているのかよくわかりません:

次のようにテキスト幅を測定できます: (ここで Alan Jay Weiner から取得: Auto Scale TextView Text to Fit within Bounds )

// Set the text size of the text paint object and use a static layout to render text off screen before measuring
private int getTextWidth(CharSequence source, TextPaint paint, int width, float textSize) {
    // Update the text paint object
    paint.setTextSize(textSize);
    // Draw using a static layout
    StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true);
    layout.draw(sTextResizeCanvas);
    return layout.getWidth();
}  

次のようなウォッチャーを使用して、変更されたテキストを測定できます。

 yourEditText.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //measure your stuff here
            }
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
于 2013-04-22T14:47:16.057 に答える