java - 将当前 Activity 实例传递给方法

标签 java android

我有一个按钮,其部分功能位于一个类中,该类位于同一包中的另一个类文件中。这是应该如何完成的方式,还是我可以以某种方式传递当前 Activity 的实例,然后使用它在第二类的方法中具有的任何方法或字段。

public class LogInScreen extends AppCompatActivity {
    Button logInButton;

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

        logInButton = findViewById(R.id.button_login);
        userText = findViewById(R.id.editText_user);
        passText = findViewById(R.id.editText_pass);
    }

    public void logIn(View view) {
        logInButton.setEnabled(false);

        String username = userText.getText().toString();
        String password = passText.getText().toString();

        BackendConnectionService.encodeCredentials(username, password);

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        BackendConnectionService.handleSSLHandshake();
        // here I put the button
        requestQueue.add(BackendConnectionService.createLogInRequest(this, logInButton));
    }
}

第二类声明

public class BackendConnectionService {
    // some class declarations


    static JsonArrayRequest createLogInRequest(Context packageContext, Button button){
        final Context context = packageContext;
        final Button b = button;
        return new JsonArrayRequest(
                Request.Method.GET,
                PALLETE_URL,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Intent intent = new Intent(context, PalletsScreen.class);
                        context.startActivity(intent);
                        b.setEnabled(true);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("REST ResponseErr", error.toString());
                        Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
                        b.setEnabled(true);
                    }
                }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(autorisation, encodedCredentials);
                return headers;
            }
        };
    }
}

最佳答案

说实话,你已经做到了:)。您只需要知道如何使用它即可。假设您想在 createLogInRequest 方法中使用 LogInScreen:

static JsonArrayRequest createLogInRequest(Context packageContext, Button button){
    final Context context = packageContext;
    final Button b = button;

    LogInScreen logInScreen = null;
    if(context instanceof LogInScreen) {
        logInScreen = (LogInScreen) context;
    }
    //...
}

关于java - 将当前 Activity 实例传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48411204/

相关文章:

java - RichFaces 3.3 : Calling actionListener on window. 加载

java - JProgressBar.setMaximum(int) 有时似乎不起作用

java - 如何使用Jemmy获取java表格行/单元格/文本的颜色、字体?

java - 从面板切换到 tabbedPane

java - Hibernate,尝试访问孙子字段一对多关系

android - 导出 C 局部变量 : JNI run-time behavior issue

java - android SqLite AutoCompleteTextView

java - 如何构建编程过程?

android - 更改 ListView 上所选项目的背景颜色

android - 零星的 IllegalArgumentException : Unknown URL content://