2

私は初心者で、いくつかのアクティビティを持つ単純な Android アプリを作成しています。各アクティビティでは、進行状況ダイアログとカスタム レイアウトのトーストを使用する必要があります。また、いくつかの環境設定を保存してロードする必要があります。これらすべてのメソッドを別のクラスに配置するにはどうすればよいですか。すべてのアクティビティで同じコードを記述したくありません。静的クラスにすることはできますか?

ありがとうございました。

    private void createCustomToast(String msg, String status) {
    Context context = getApplicationContext();
    LayoutInflater inflater = getLayoutInflater();
    View toastRoot = inflater
            .inflate(R.layout.custom_toast_two_lines, null);
    TextView text = (TextView) toastRoot.findViewById(R.id.toastText);
    TextView textTriageStatus = (TextView) toastRoot
            .findViewById(R.id.status);
    textTriageStatus.setText(status);
    text.setTextColor(Color.BLACK);
    text.setText(msg);
    Toast toast = new Toast(context);
    toast.setView(toastRoot);
    toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
    toast.show();
}

private void savePreferences(String key, int value) {
    SharedPreferences sharedPreferences = getSharedPreferences(
            "APP_PREFERENCES", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
}

private String loadPreferences(String key) {
    SharedPreferences sharedPreferences = getSharedPreferences(
            "APP_PREFERENCES", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString(key, "");
    return strSavedMem1;
}
4

1 に答える 1

0

この種のシナリオを処理する別のクラスを作成できますが、トーストと設定はコンテキストによって処理されるため、そのメソッドにコンテキストを渡す必要があります。コンテキストには、1) アプリケーション、2) アクティビティ、3) サービス、4) ブロードキャスト レシーバーなど、さまざまなタイプがあります。

> >  public static void createCustomToast(Context context,String msg, String status) {
>     >     LayoutInflater inflater = getLayoutInflater();
>     >     View toastRoot = inflater
>     >             .inflate(R.layout.custom_toast_two_lines, null);
>     >     TextView text = (TextView) toastRoot.findViewById(R.id.toastText);
>     >     TextView textTriageStatus = (TextView) toastRoot
>     >             .findViewById(R.id.status);
>     >     textTriageStatus.setText(status);
>     >     text.setTextColor(Color.BLACK);
>     >     text.setText(msg);
>     >     Toast toast = new Toast(context);
>     >     toast.setView(toastRoot);
>     >     toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
>     >     toast.show(); }
>     > 
>     > public static void savePreferences(Context context,String key, int value) {
>     >     SharedPreferences sharedPreferences = context.getSharedPreferences(
>     >             "APP_PREFERENCES", MODE_PRIVATE);
>     >     SharedPreferences.Editor editor = sharedPreferences.edit();
>     >     editor.putInt(key, value);
>     >     editor.commit(); }
>     > 
>     > public void String loadPreferences(Context context,,String key) {
>     >     SharedPreferences sharedPreferences = context.getSharedPreferences(
>     >             "APP_PREFERENCES", MODE_PRIVATE);
>     >     String strSavedMem1 = sharedPreferences.getString(key, "");
>     >     return strSavedMem1; }
>     > 
>     > > Blockquote
于 2013-07-20T18:22:37.730 に答える