我有一个公共类,有一个public static String
方法。我必须getString()
并且我知道在此之前我必须提取String
ressource并传递Context并且我不知道如何传递它。
另一个班级使用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类使用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类'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 Code:
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);
}
}
答案 0 :(得分:1)
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
答案 1 :(得分:0)
用这个替换你的StringUtil类。
import java.io.*;
class tictactoe
{
BufferedReader xy=new BufferedReader(new InputStreamReader(System.in));
void accept()throws IOException
{
System.out.println("Enter your weapon X or O : ");
char ch = xy.readLine().charAt(0);
System.out.println("Your move:");
String s=xy.readLine();
}
}
从ButtonListener活动中调用此方法。
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;
}
}
答案 2 :(得分:0)
你有两种方式: 1.将其作为方法的参数传递:
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.向类构造函数添加上下文,然后您可以在方法中使用它:
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;
}
}