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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="@+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="@+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
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;
}
});