将编辑文本放在文本字符串中间的最佳方法是什么?

时间:2014-03-04 13:24:22

标签: android android-edittext textwrapping

我想在空白活动中创建一个填充 例: “昨天的天气(编辑文字在这里)。” “男孩们在休息期间玩(编辑文字)。” “露西昨天在午餐时喝酒(编辑文字)。”

我现在设置它的方式,我在编辑文本之前和之后都有一个文本视图。问题是文本视图的文本是由意图发送的putStringExtra方法设置的。我永远不知道任何一个视图的文本会有多长。如果文本太长,则第二个视图的文本会在最后聚集。也就是说,文本包装在第二个文本视图上。我想要实现的是整个屏幕环绕的文本。目前,我正在检查第二个文本视图的行数。如果有多个,那么我将该文本放在第一个布局下面的布局中的textview中。似乎有一种更简单的方法可以做到这一点。这是我的代码,谢谢你的帮助:

 public void setText(double w, double t) {
    final double HalfScreenWidth = w;
    final double totalScreenWidth = t;
    TextView1.setText(getIntent().getExtras().getString("SentPart1"));
    TextView2.setText(getIntent().getExtras().getString("SentPart2"));
    TextView3.setText("");


    TextView1.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    tVWidth = TextView1.getMeasuredWidth();

    if (totalScreenWidth > 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();

        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay1.addView(TextView2);
        linLay1.addView(TextView3);
    } else if (tVWidth > HalfscreenWidth && totalScreenWidth < 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();

        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay2.addView(TextView2);
    } else if (tVWidth < HalfScreenWidth && totalScreenWidth < 1100) {
        linLay1.removeAllViews();
        linLay2.removeAllViews();
        linLay1.setVisibility(View.INVISIBLE);
        linLay2.setVisibility(View.INVISIBLE);
        linLay1.addView(TextView1);
        linLay1.addView(EditText);
        linLay1.addView(TextView2);
        linLay2.addView(TextView3);
        h.postDelayed(r, 100);

    }

}

final Runnable r = new Runnable() {
    public void run() {

        int lineCount = TextView2.getLineCount();
        if (lineCount == 1) {
            linLay2.removeView(TextView3);
            linLay1.addView(TextView3);
        } else if (lineCount == 2) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart = TextView2.getLayout().getLineStart(1);
            int lineEnd = TextView2.getLayout().getLineEnd(1);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText =  TextView2.getText().subSequence(lineStart,
                    lineEnd);
            TextView2.setText(extraText0);
            TextView3.setText(extraText);

        } else if (lineCount == 3) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart1 = TextView2.getLayout().getLineStart(1);
            int lineEnd1 = TextView2.getLayout().getLineEnd(1);
            int lineStart2 = TextView2.getLayout().getLineStart(2);
            int lineEnd2 = TextView2.getLayout().getLineEnd(2);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText1 = TextView2.getText().subSequence(
                    lineStart1, lineEnd1);
            CharSequence extraText2 = TextView2.getText().subSequence(
                    lineStart2, lineEnd2);

            CharSequence oneTwo = TextUtils.concat(extraText1, extraText2);
            TextView2.setText(extraText0);
            TextView3.setText(oneTwo);

        } else if (lineCount == 4) {
            int lineStart0 = TextView2.getLayout().getLineStart(0);
            int lineEnd0 = TextView2.getLayout().getLineEnd(0);
            int lineStart1 = TextView2.getLayout().getLineStart(1);
            int lineEnd1 = TextView2.getLayout().getLineEnd(1);
            int lineStart2 = TextView2.getLayout().getLineStart(2);
            int lineEnd2 = TextView2.getLayout().getLineEnd(2);
            int lineStart3 = TextView2.getLayout().getLineStart(3);
            int lineEnd3 = TextView2.getLayout().getLineEnd(3);

            CharSequence extraText0 = TextView2.getText().subSequence(
                    lineStart0, lineEnd0);
            CharSequence extraText1 = TextView2.getText().subSequence(
                    lineStart1, lineEnd1);
            CharSequence extraText2 = TextView2.getText().subSequence(
                    lineStart2, lineEnd2);
            CharSequence extraText3 = TextView2.getText().subSequence(
                    lineStart3, lineEnd3);

            CharSequence oneTwo = TextUtils.concat(extraText1, extraText2,
                    extraText3);
            TextView2.setText(extraText0);
            TextView3.setText(oneTwo);
 }
linLay1.setVisibility(View.VISIBLE);
        linLay2.setVisibility(View.VISIBLE);
}

我输入了一个runnable,因为看起来Android需要一点停顿才能在屏幕上绘制文字。

2 个答案:

答案 0 :(得分:0)

您可以横向使用LinearLayout并将TextView放在EditText的两侧。像这样:

<LinearLayout 
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The weather was " /> <!-- Just for short answer, should be put elsewhere -->
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" yesterday." /> <!-- Just for short answer, should be put elsewhere -->
</LinearLayout>

在您的代码中,您可以设置如下文本:

// Written here as if it where put in and Activity
public void setYesterdayWeather(final String text) {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            TextView v = (TextView) findViewById(R.id.myEditText);
            v.setText(text);
        }
    });
}

之后,您可以使用TextWatcher收听更改并调整EditText的大小,以便它始终包含其文本:

// On inflation, likely in Activity.onCreate()
final TextView v = (TextView) findViewById(R.id.myEditText);
v.addTextChangedListener(new TextWatcher() {

    [...]

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        float textSize = v.getPaint().measureText(s, start, start + count);
        // onTextChanged may run on Ui Thread, but if it's not the case, the following statement must be called inside a Runnable posted to the main Looper.
        v.setWidth(textSize); // Eventually, take some extra space for padding ?
    }
});

答案 1 :(得分:0)

您可以在活动中设置它:

String chaine ="The weather was"+getIntent().getExtras().getString("your String name")+"yesterday." ;
        TextView text = (TextView) findViewById(R.id.text);
text.setText(chaine);

在XML中: