以下のテキストを持っています。
「私はお茶とコーヒーを飲みます」。ここで、このテキストを「私はEditText
ANDを飲みます」に変更する必要があります。EditText
ここで、EditTextは、クリックするとユーザーが回答を入力できる編集ボックスです。この変更をプログラムで行う必要があります。
これをどのように達成できるかについて、これに関する提案はありますか????
以下のテキストを持っています。
「私はお茶とコーヒーを飲みます」。ここで、このテキストを「私はEditText
ANDを飲みます」に変更する必要があります。EditText
ここで、EditTextは、クリックするとユーザーが回答を入力できる編集ボックスです。この変更をプログラムで行う必要があります。
これをどのように達成できるかについて、これに関する提案はありますか????
レイアウト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" >
<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;
}
});
ボタンのクリック イベント ハンドラで次のコードを使用できます。
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
それぞれ「私は飲む」、「お茶」、「」、「コーヒー」というテキストを含む 4 つのテキストボックスを使用できます。2番目と4番目のテキストボックスのontouchイベントでは、テキストボックスを表示したり、テキストを編集したり、テキストを編集したりできます。ボタンをクリックすると、テキストボックスからテキストを取得して再度表示できます。
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() メソッドに入れます
を使用しViewSwitcher
ます。このウィジェットには、次の 2 つのビューのいずれかが表示されます。
<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>
次に、ボタンが押されると、ボタンを切り替えてテキストをロードしEditText
ますTextView
。
editText.setText(textView.getText());
viewswitcher.showNext();
また
textView.setText(editText.getText());
viewswitcher.showPrevious();
詳しくはこちらをご覧ください。
これを使用するだけです:yourTextField.setText("..."+yourEditText.getText().toString());
使用することで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");