Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式添加编辑文本框_Android_Android Edittext - Fatal编程技术网

Android 以编程方式添加编辑文本框

Android 以编程方式添加编辑文本框,android,android-edittext,Android,Android Edittext,我随身携带以下文本 “我喝茶和咖啡。”。现在要求将此文本更改为“我喝EditText和EditText”……这里的EditText是一个编辑框,用户可以在其中单击后输入答案。我需要按专业语法进行更改 关于这方面的任何建议,以及如何通过使用getText() 范例 Button mButton; EditText mEdit; /** Called when the activity is first created. */ @Override public void onCreate(Bu

我随身携带以下文本

我喝茶和咖啡。”。现在要求将此文本更改为“我喝
EditText
EditText
”……这里的EditText是一个编辑框,用户可以在其中单击后输入答案。我需要按专业语法进行更改

关于这方面的任何建议,以及如何通过使用
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");

只需使用以下命令:
yourTextField.setText(“…”+yourEditText.getText().toString())


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

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

String str=“我喝茶和咖啡”
String editTextString=editText.getText().ToString()
str.replace(“coffee”,editTextString)

str.replace(“tea”,editTextString)

使用
视图切换器
。此小部件显示其包含的两个视图之一:

    <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>


有关详细信息,请参阅。

您可以使用4个文本框,分别显示“我喝”、“茶”和“咖啡”。
在第二个和第四个文本框的ontouch事件中,您可以显示文本框或编辑文本并编辑文本。单击按钮,您可以从文本框中获取文本并再次显示。

您可以在布局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" >

并将listners设置为您的按钮

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;
        }
    });
textView.setText(editText.getText());
viewswitcher.showPrevious();    
<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;
        }
    });