安卓:显示字符串的TextView [英] Android: Display string with textView
本文介绍了安卓:显示字符串的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我试图显示与的TextView
的字符串。我这样做动态,这是code:
的LinearLayout布局=(的LinearLayout)this.findViewById(R.id.Layout);
EDITTEXT的EditText =(EditText上)this.findViewById(R.id.textView);
Button按钮=(按钮)this.findViewById(R.id.btnAdd); 的LayoutParams lparams =新的LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView的电视=新的TextView(本);
tv.setLayoutParams(lparams);
tv.setText(显示);
this.layout.addView(电视);
,但它击中 this.layout.addView(TV)的错误;
能否请你告诉我什么我做错了。
非常感谢事先!
更新(充分活动code):
公共类CalendarDate延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_calendar_date);
捆绑额外= getIntent()getExtras()。 串d日= extras.getString(CURRENTDAY);
串d月= extras.getString(currentmonth);
串d年= extras.getString(currentyear); 动作条AB = getActionBar();
ab.setTitle(d日+/+ d月+/+ d年); 捆绑extraz = getIntent()getExtras()。
字符串显示= extras.getString(EXTRA_MESSAGE); 的setContentView(R.layout.activity_calendar_date); 的LinearLayout布局=(的LinearLayout)this.findViewById(R.id.Layout);
EDITTEXT的EditText =(EditText上)this.findViewById(R.id.textView);
Button按钮=(按钮)this.findViewById(R.id.btnAdd); 的LayoutParams lparams =新的LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView的电视=新的TextView(本);
tv.setLayoutParams(lparams);
tv.setText(显示);
layout.addView(电视);
}
解决方案
删除这个
,只 layout.addView(TV);
。使用这个
用于引用唯一实例变量。 (在任何方法类的外部变量)
hello i am trying to display a string with a TextView
. i am doing this dynamically and this is the code:
LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
EditText editText=(EditText) this.findViewById(R.id.textView);
Button button=(Button) this.findViewById(R.id.btnAdd);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(display);
this.layout.addView(tv);
,but it hits an error on this.layout.addView(tv);
Can you please tell me what i am doing wrong?
Thanks a lot in advance!
Update(full activity code):
public class CalendarDate extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_date);
Bundle extras = getIntent().getExtras();
String dday = extras.getString("currentday");
String dmonth =extras.getString("currentmonth");
String dyear = extras.getString("currentyear");
ActionBar ab = getActionBar();
ab.setTitle(dday+"/"+dmonth+"/"+dyear );
Bundle extraz =getIntent().getExtras();
String display = extras.getString("EXTRA_MESSAGE");
setContentView(R.layout.activity_calendar_date);
LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
EditText editText=(EditText) this.findViewById(R.id.textView);
Button button=(Button) this.findViewById(R.id.btnAdd);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(display);
layout.addView(tv);
}
解决方案
Remove this
, just layout.addView(tv);
. Use this
for referencing instance variables only. (Variables in the class outside of any methods)
这篇关于安卓:显示字符串的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文