【发布时间】:2019-11-07 06:32:49
【问题描述】:
我创建了一个 java 类,但我认为如果它不适用于 getLayoutInflator() 和 getApplicationContext()
我该如何解决这个问题,这就是我所拥有的
public final class CustomFunctions {
/**
* Private constructor to prevent instantiation
*/
private CustomFunctions() {}
public static void customToast(String msg){
/*Custom Toast Message*/
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(msg);
Button btnDismiss = (Button) layout.findViewById(R.id.btndismiss);
final Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
hideDialog();
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toast.cancel();
}
});
/*Custom Toast Message*/
}
}
【问题讨论】:
-
只需将上下文传递给类,您就可以使用
getLayoutInflator和getApplicationContext