java Android:如何将变量应用于吐司?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15931553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to apply a variable to a toast?
提问by user2261396
I'm going to be using a toast in my application for testing purposes. I am only new to the Android environment and I'm not very familiar with toasts. I know a standard toast it like this: Toast.makeText(context, text, duration).show();
. However, instead of applying a String of text into the 'text' section, I want to apply a variable.
我将在我的应用程序中使用 toast 进行测试。我只是Android环境的新手,对toasts不是很熟悉。我知道它敬酒一个标准是这样的:Toast.makeText(context, text, duration).show();
。但是,我想应用一个变量,而不是将文本字符串应用到“文本”部分。
Here is what I have written:
这是我写的:
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
is a variable used in a different method present in the class. Any suggestions on how I can get the toast to contain the contents of cText
? Thanks in advance.
cText
是在类中存在的不同方法中使用的变量。关于如何让吐司包含内容的任何建议cText
?提前致谢。
回答by edwin
May be you an Try this
可能是你试试这个
public class MainActivity extends Activity {
String Text="MainActivity Message"; //Global variable
@Override
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) {
//Declaring two variables.
//You can also declare it as global.
//but global variable must be initialized before creating toast otherwise you will get NPE and lead to you application crash
String cText="Toast Message";
int val=1;
Toast.makeText(getApplicationContext(), cText, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "vlaue is "+val, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), getMessage(), Toast.LENGTH_LONG).show();
}
});
}
public String getMessage(){
return "Text from Function";
}
}
回答by Raghunandan
Declare cText with a class scope. setTextvalue() sets the string value. On Button click call displayValue() to display the toast message with the value set to cText.
使用类范围声明 cText。setTextvalue() 设置字符串值。在按钮上单击调用 displayValue() 以显示值设置为 cText 的 toast 消息。
public class MainActivity extends Activity {
String cText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTextvalue();
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayValue();
}
});
}
public void setTextvalue()
{
cText="hello";
}
public void displayValue()
{
Toast.makeText(MainActivity.this, cText.toString(), Toast.LENGTH_LONG).show();
}
}
回答by Vishal Sharma
Try:
尝试:
String message = "hello";
toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
toast.show();
TextView tv = (TextView)view.findViewById(android.R.id.message);
String strMessage = tv.getText().toString();
回答by Alexander Surdushkin
Try this snippet. I know that question was asked a while ago, however, it might be useful for someone else. That helped me to put the desired variable inside a Toast message.
试试这个片段。我知道这个问题是不久前提出的,但是,它可能对其他人有用。这帮助我将所需的变量放入 Toast 消息中。
public void clickMeButton(View view) {
EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
Toast.makeText(this,nameEditText.getText().toString() ,Toast.LENGTH_SHORT).show();
}
回答by meredrica
Looks like cText is out of scope. Either define it in the Top level or as a final variable before setting up the onClickListener.
看起来 cText 超出了范围。在设置 onClickListener 之前,将其定义在顶层或作为最终变量。
You should learn the java basics about variables before diving head first into android, that will help you a lot. I can recommend the Head First Java book for that.
在深入研究 android 之前,您应该学习有关变量的 Java 基础知识,这将对您有很大帮助。我可以为此推荐 Head First Java 书。