Android: 如何将变量应用于Toast?

5

我将在我的应用程序中使用一个toast来进行测试。我刚接触Android环境,对toast不是很熟悉。我知道标准的toast如下:Toast.makeText(context, text, duration).show();。然而,我想要在“text”部分应用一个变量,而不是一个文本字符串。

这是我写的代码:

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是在类中的另一个方法中使用的变量。您有什么建议,我如何让toast包含cText的内容?提前致谢。


cText的类型是什么?它是否局限于其他方法? - jlordo
什么类型的变量?一个Int吗?使用Toast.makeText(getApplicationContext(),“Value:”+ yourInt,Toast.LENGTH_LONG).show()。 - RyPope
简单来说,cText超出了范围,你将无法完成这个任务。你可以将cText存储为全局变量并引用该变量,但这通常不被认为是良好的实践。 - Colton
Toast.makeText(getApplicationContext(), cText.toString(), Toast.LENGTH_LONG).show(); - Joao Guilherme
6个回答

2
也许你可以尝试这个。
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";
  }
}

0

试试这个代码片段。我知道这个问题已经很久了,但它可能对其他人有用。这帮助我将所需变量放在 Toast 消息内。

   public void clickMeButton(View view) {
    EditText nameEditText = (EditText) findViewById(R.id.nameEditText);

   Toast.makeText(this,nameEditText.getText().toString() ,Toast.LENGTH_SHORT).show();
    }

0

在类范围内声明cText。setTextvalue()设置字符串值。点击按钮时调用displayValue()来显示toast消息,其中的值设置为cText。

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();
 }
}

0

看起来 cText 超出了范围。要么在顶级中定义它,要么在设置 onClickListener 之前将其定义为最终变量。

在深入研究 Android 之前,应该先学习 Java 变量的基础知识,这会对你很有帮助。我可以推荐 Head First Java 这本书。


0

cText听起来像是一个字符序列或者至少是某种文本。假设它是字符序列或字符串:是的,你可以使用它。 此外,你还可以自定义你的弹出通知。


0

尝试:

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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接