android - 在 Android 库中隐藏未使用的公共方法
问题描述
我正在创建一个 Android 函数库,我将在我正在创建的一系列应用程序中使用它。我正在使用 Android Studio 来创建这些应用程序。
我在库中添加了三种方法。库不使用这些方法,仅使用该库的应用程序。虽然这些应用程序仍然可以正常编译和工作,但我非常注意在源代码中收到警告。我知道 @SupressWarning("unused") 注释,但这仅适用于私有方法,并且由于方法在库中需要公开,因此对我不起作用。
有没有办法可以在公共方法的源中抑制未使用的警告?我想你可以说,当我在 Android Studio 中看到绿色箭头时,我感到很舒服,这表明我没有错误。
解决方案
我确实想出了一个解决方案,不确定它是否是正确/最佳的解决方案。我在调用每个成员方法的类中创建了一个构造函数。这消除了编辑器中的警告。
package com.phoenixhosman.phoenixlib;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import org.jetbrains.annotations.NotNull;
import static android.view.View.inflate;
public class ActivityPhoenixLib extends AppCompatActivity {
/**
* While not needed, this constructor makes calls to the member method of the class
* to clear errors in the editor.
*/
ActivityPhoenixLib() {
String dummy = InputDialog(this,"");
if (dummy.equals("test")) {
Error(this, "", false);
Success(this,"",0);
}
}
/**
* This method displays a dialog box with an error message and a close button.
* @param strError the error message to display
*/
public void Error(Context context, @NotNull String strError, Boolean exit) {
if (strError.equals("")) {
return;
}
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
View view = inflate(context, R.layout.dialog_error, null);
Button btnExit = view.findViewById(R.id.btnExitButton);
Button btnError = view.findViewById(R.id.btnErrorMessage);
btnError.setText(strError);
mBuilder.setView(view);
AlertDialog dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
btnExit.setOnClickListener(v -> {
dialog.dismiss();
if (exit) System.exit(0);
});
}
/**
* This method displays a dialog box with a message and a close button.
* It also has an auto close function to auto close after a specified number of seconds.
* @param strMessage the message to display
*/
public void Success(Context context, @NotNull String strMessage, Integer autoclose) {
if (strMessage.equals("")) {
return;
}
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
View view = inflate(context, R.layout.dialog_success, null);
Button btnExit = view.findViewById(R.id.btnButton);
Button btnError = view.findViewById(R.id.btnMessage);
btnError.setText(strMessage);
mBuilder.setView(view);
AlertDialog dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
btnExit.setOnClickListener(v -> dialog.dismiss());
new Handler().postDelayed(dialog::dismiss, autoclose * 1000);
}
/**
* This method show an input box to get string input from the user.
* @param strPrompt - the dialog prompt
*/
public String InputDialog(Context context, @NotNull String strPrompt) {
if (strPrompt.equals("")) {
return "";
}
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
final String[] retval = new String[1];
View view = inflate(context, R.layout.dialog_input, null);
TextView txtPrompt = view.findViewById(R.id.txtPrompt);
EditText edtLockPass = view.findViewById(R.id.edtLockPass);
Button btnEnter = view.findViewById(R.id.btnEnter);
txtPrompt.setText(getString(R.string.input_prompt, strPrompt));
mBuilder.setView(view);
AlertDialog dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
btnEnter.setOnClickListener(v -> {
if (edtLockPass.getText().toString().equals("")) {
retval[0] = "";
} else {
retval[0] = edtLockPass.getText().toString();
}
});
dialog.dismiss();
return retval[0];
}
}
有没有更好的方法来实现我的结果?
推荐阅读
- android - MaterialContainerTransform 未解析的引用
- react-admin - 登录后 React admin 应该重定向到主页,但它会重定向到上一个 url
- botframework - 如何确定该 Web 应用程序 (html/js) 在任务模块中打开?
- r - 基于另一个数据框从数据框中提取行
- r - 如何在 R 中创建 3D 曲面?
- java - 使用 Pax-Exam 进行 OSGi 测试失败,没有找到容器错误
- java - 为什么图像不能在 JAVAFX 中加载
- shell - 目录的 cp 命令出现问题
- python - 打开金字塔图像 ob google collab:large_image getTileSource 在 google collab 上不起作用(对于 ndpi 没有可用的 tilesource)
- ios - 将 UILabel 中的文本放在表视图单元格内