【发布时间】:2013-04-10 16:36:09
【问题描述】:
我将在我的应用程序中使用 toast 来进行测试。我只是 Android 环境的新手,对 toast 不是很熟悉。我知道标准吐司是这样的:Toast.makeText(context, text, duration).show();。但是,我不想将文本字符串应用到“文本”部分,而是应用一个变量。
这是我写的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_next);
Button send = (Button) findViewById(R.id.bSend);//Import button1 (Send)
send.setOnClickListener(new OnClickListener() {//Set an onClickListener for the button to work
public void onClick(View v) {
Toast.makeText(getApplicationContext(), cText, Toast.LENGTH_LONG).show();
}//end method
});//End Send
}//End onCreate
cText 是在类中存在的不同方法中使用的变量。关于如何让吐司包含cText 的内容的任何建议?提前致谢。
【问题讨论】:
-
cText的类型是什么?它是另一种方法的本地方法吗? -
什么样的变量?一个整数?使用 Toast.makeText(getApplicationContext(), "Value:" + yourInt, Toast.LENGTH_LONG).show()
-
简单地说 cText 超出范围,您将无法执行此操作。您可以做的是全局存储 cText 并引用该变量,但这通常不是好的做法。
-
Toast.makeText(getApplicationContext(), cText.toString(), Toast.LENGTH_LONG).show();