开发者

Android passing variable

开发者 https://www.devze.com 2023-02-05 01:19 出处:网络
I want to pass the content of an edittext to a notification in another .java file. The main part of the code is as follows:

I want to pass the content of an edittext to a notification in another .java file. The main part of the code is as follows:

   public class HelloAndroid2 extends Activity {  

private Button b2;
public Editable etext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   

    EditText editText = (EditText)findViewById(R.id.EditText01);
    editText.setText("name");
    etex开发者_运维知识库t = editText.getText();

    b2 = (Button) findViewById(R.id.Button02);
    b2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      //Toast.makeText(HelloAndroid2.this, editText.getText(), Toast.LENGTH_LONG).show();
        Toast.makeText(HelloAndroid2.this, etext, Toast.LENGTH_LONG).show();

    }
    });

}

}

This should be code for calling the etext variable in my MyService.java file: String MyNotifiyText = etext; What should i do, or how should i call the etext variable in the other .java file?


You can either use a static variable across the two activities or if MyService is calling HelloAndroid2 you can pass the data using intent with extras.


The easiest way is to create a model class, which will holds all data.

e.g.:

package com.examples;

public class DataModel {

    public static String txt = "";

}

Yo should put your data in this variable as follows:

DataModel.txt = "you string"; //editText.getText();

And you can access it from any where:

editText.setText(DataModel.txt);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号