在 textView 上显示来自 editText 字段的用户输入
Display users input from editText field on a textView
我使用下面的代码在 editText
上使用 getText();
并希望用户输入替换 textView
那么我该怎么做?
顺便说一句,代码取自这个问题:
Get Value of a Edit Text field
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());
}
});
}
1- 找到你的 textView
TextView txt = (TextView) findViewById(R.id.yourTextView);
2- 从 EditText 获取文本后将其设置为 TextView :
txt.setText(mEdit.getText().toString());
Button mButton;
EditText mEdit;
TextView mText;
/** 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);
mText = (TextView) findViewById(R.id.textview1);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
mText.setText(mEdit.getText.toString());
}
});
}
我认为 TextView 数组对你来说会很好。
TextView [] myTexts = new TextView[6];
myTexts[0]=findViewById(R.id.txt1);
myTexts[1]=findViewById(R.id.txt2);
...
myTexts[5]=findViewById(R.id.txt6);
int questionNumber = 0;
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
}
});
questionNumber 整数将保存要回答的问题的值,但如果您不希望 ArrayIndexOutOfRange 异常,请不要让它的值大于 5。
我使用下面的代码在 editText
上使用 getText();
并希望用户输入替换 textView
那么我该怎么做?
顺便说一句,代码取自这个问题:
Get Value of a Edit Text field
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());
}
});
}
1- 找到你的 textView
TextView txt = (TextView) findViewById(R.id.yourTextView);
2- 从 EditText 获取文本后将其设置为 TextView :
txt.setText(mEdit.getText().toString());
Button mButton;
EditText mEdit;
TextView mText;
/** 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);
mText = (TextView) findViewById(R.id.textview1);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
mText.setText(mEdit.getText.toString());
}
});
}
我认为 TextView 数组对你来说会很好。
TextView [] myTexts = new TextView[6];
myTexts[0]=findViewById(R.id.txt1);
myTexts[1]=findViewById(R.id.txt2);
...
myTexts[5]=findViewById(R.id.txt6);
int questionNumber = 0;
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
}
});
questionNumber 整数将保存要回答的问题的值,但如果您不希望 ArrayIndexOutOfRange 异常,请不要让它的值大于 5。