如何使用 public 静态字符串方法将上下文传递给 public class

How to pass Context into public class with public static String method

我有一个 public class 和一个 public static String 方法。我必须 getString() 并且我知道在那之前我必须提取 String 资源并传递 Context 但我不知道如何传递它。

另一个class使用public static String方法

代码示例:

public class StringUtil {

    public static String sumUp(int nr1, int nr2) {
        int sum = nr1 + nr2;
        String result;

        //all what is in ""  
        result =.getString(R.string.result) + sum;
        return result;

    }
}

ButtonListener class 使用 public static String 方法。代码:

public class ButtonListener extends MainActivity implements View.OnClickListener {
MainActivity activity;
EditText et1;
EditText et2;

public ButtonListener(MainActivity activity) {
    this.activity = activity;
}

@Override
public void onClick(View view) {

    switch (view.getId()){
        case R.id.sum:
            et1 = (EditText) activity.findViewById(R.id.nr1_edit_text);
            et2 = (EditText)activity.findViewById(R.id.nr2_edit_text);

            String nr1In = et1.getText().toString();
            String nr2In = et1.getText().toString();

            int nr1 = Integer.parseInt(nr1In);
            int nr2 = Integer.parseInt(nr2In);

            Intent intent = new Intent(activity, IntentActivity.class);
            intent.putExtra("result", StringUtil.sumUp(nr1, nr2));
            activity.startActivity(intent);
            break;
    }

}
}

IntentActivity class“getIntent”代码:

public class IntentActivity extends MainActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);


       TextView textView = (TextView)findViewById(R.id.result);

        Intent intent = getIntent();

        String fResult = intent.getStringExtra("result");
        textView.setText(fResult);


    }
}

和 MainActivity 代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        ButtonListener bl = new ButtonListener(this);

        Button sumUp= (Button)findViewById(R.id.sum_button);
        sumUp.setOnClickListener(bl);

    }
}
public class StringUtil {

    public static String sumUp(int nr1, int nr2, Context context){
        int sum = nr1 +nr2;
        String result;

        result = context.getString(R.string.result) + sum;
        return result;

        }
    }

并像这样使用它:

StringUtil.sumUp(1,2, this); // from Activity
StringUtil.sumUp(1,2, getContext()); // from Fragment

用这个替换你的 StringUtil class。

public class StringUtil {

    public static String sumUp(Context mContext, int nr1, int nr2){
        int sum = nr1 +nr2;
        String result;

        //all what is in ""  
        result = mContext.getString(R.string.result) + sum;
        return result;

    }
}

编辑

并从您的 ButtonListener activity.

调用此方法
intent.putExtra("result", StringUtil.sumUp(ButtonListener.this, nr1, nr2));

你有两种方法: 1.to 将其作为参数传递给您的方法:

          public static String sumUp(int nr1, int nr2 ,Context context){
            int sum = nr1 +nr2;
            String result;
            //all what is in ""  
            result = .getString(R.string.result) + sum;
            return result;              
            }

2.to 将上下文添加到 class 构造函数,然后您可以在您的方法中使用它:

public class StringUtil {
  Context context;

  public void StringUtil (Context context){
  this.context = context;
  }


    public static String sumUp(int nr1, int nr2){
    int sum = nr1 +nr2;
    String result;
    //all what is in ""  
    result = .getString(R.string.result) + sum;
    return result;

    }
}