我使用下面的代码在getText();
上使用editText
,并希望用户输入替换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());
}
});
}
答案 0 :(得分:2)
1-找到你的textView
TextView txt = (TextView) findViewById(R.id.yourTextView);
2-从EditText获取文本后,将其设置为TextView:
txt.setText(mEdit.getText().toString());
答案 1 :(得分:0)
我认为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。
答案 2 :(得分:-1)
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());
}
});
}