1

我有以下文字。

我喝茶和咖啡”。现在的要求是将此文本更改为“我喝酒EditText并且EditText”....这里的EditText是一个编辑框,用户可以在单击后输入答案。我需要以编程方式进行此更改。

对此有何建议,如何实现????

4

7 回答 7

1

您可以在布局 xml 文件中创建活动结构:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" >

&lt;TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="I drink " />

&lt;Button
            android:id="@+id/firstAnswer" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="EditText" />

&lt;TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text=" and " />

&lt;Button
             android:id="@+id/secondAnswer" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="EditText" />

&lt;/LinearLayout&gt;

并为您的按钮设置监听器

private String[] answers = { "tea", "coffee", "juice", "compote" };
    ...
    Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
    firstAnswer.setOnClickListener(new OnClickListener() {
        private int position = 0;
        @Override
        public void onClick(View view) {
            ((Button) view).setText(answers[position]);
            position++;
            if (position == answers.length)
                position = 0;
        }
    });
于 2013-02-25T15:11:01.520 回答
1

您可以在按钮的单击事件处理程序中使用以下代码:

String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);

于 2012-12-05T12:58:53.703 回答
0

You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively. On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.

于 2012-12-05T13:11:58.270 回答
0

使用ViewSwitcher. 此小部件显示它包含的两个视图之一:

    <ViewSwitcher
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </ViewSwitcher>

然后,当按下按钮时,将其切换并从EditTextto加载文本,TextView反之亦然:

editText.setText(textView.getText());
viewswitcher.showNext();

或者

textView.setText(editText.getText());
viewswitcher.showPrevious();    

有关详细信息,请参阅

于 2012-12-05T12:59:18.257 回答
0

通过使用getText()

例子

Button   mButton;
EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.edittext);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText", mEdit.getText().toString());
            }
        });
}

在那之后

 mEdit.setText("Tea");
于 2012-12-05T12:52:42.620 回答
0

只需使用这个:yourTextField.setText("..."+yourEditText.getText().toString());

于 2012-12-05T12:54:08.613 回答
0
 EditText et=(EditText)findViewByID(R.id.yourId);
 Button bt=(Button)findViewById(R.id.yourBtId);
 bt.setOnClickListener(
              new View.setOnClickListener()
              {
               public void onClick(View v)
               {
                  et.setText("You Drink EditText");
                }
               });

将这些代码放在 onCreate() 方法中

于 2012-12-05T12:58:36.257 回答