【问题标题】:Android: How to apply a variable to a toast?Android:如何将变量应用于吐司?
【发布时间】: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();

标签: java android


【解决方案1】:

你可以试试这个

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";
  }
}

【讨论】:

    【解决方案2】:

    看起来 cText 超出了范围。在设置 onClickListener 之前将其定义在顶层或作为最终变量。

    在开始研究 android 之前,您应该学习有关变量的 Java 基础知识,这将对您有很大帮助。我可以为此推荐 Head First Java 书籍。

    【讨论】:

      【解决方案3】:

      用类范围声明 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();
       }
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          试试:

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

          【讨论】:

            【解决方案6】:

            试试这个 sn-p。我知道这个问题是不久前提出的,但是,它可能对其他人有用。这有助于我将所需的变量放入 Toast 消息中。

               public void clickMeButton(View view) {
                EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
            
               Toast.makeText(this,nameEditText.getText().toString() ,Toast.LENGTH_SHORT).show();
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-10-21
              • 2019-03-31
              • 1970-01-01
              • 1970-01-01
              • 2015-04-23
              • 2014-05-13
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多