2015-04-23 69 views
-2

我是Java和Android的初學者,請大家幫忙。我想把Alert Dialog放到一個片段中,但我最終將大部分代碼複製到了下面的類中,因此兩個類包含幾乎相同的代碼。你能告訴我如何分離代碼而不在Fragment類中複製大部分代碼?如何在片段中顯示AlertDialog

謝謝!

public class MainActivity extends Activity { 

private int mInterval = 1000; 
private Handler mHandler; 
TextView textView; 
boolean mStarted; 
final static String simple_Date_Format = "HH:mm:ss SSS"; 

public void updateStatus() { 
    long currentTimeMillis = System.currentTimeMillis(); 
    Date date = new Date(currentTimeMillis); 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simple_Date_Format); 
    String time_now = simpleDateFormat.format(date.getTime()); 
    textView.setText(time_now); 
} 

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

    long currentTimeMillis = System.currentTimeMillis(); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(currentTimeMillis); 

    Date date = new Date(currentTimeMillis); 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simple_Date_Format); 
    String time_now = simpleDateFormat.format(date.getTime()); 
    textView = (TextView) findViewById(R.id.textview); 
    textView.setText(time_now); 

    mHandler = new Handler(); 
    startRepeatingTask(); 

    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        MainActivity.this); 

      if (mStarted) { 
       alertDialogBuilder.setTitle(R.string.settings); 
       alertDialogBuilder.setMessage(R.string.stop_message); 

      } else { 
       alertDialogBuilder.setTitle(R.string.settings); 
       alertDialogBuilder.setMessage(R.string.restart_message); 
      } 

      alertDialogBuilder.setPositiveButton(R.string.click_me, 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          if (!mStarted) { 
           startRepeatingTask(); 
          } else { 
           stopRepeatingTask(); 
          } 

         } 
        }); 

      alertDialogBuilder.setCancelable(true); 
      AlertDialog alertDialog = alertDialogBuilder.create(); 
      alertDialog.show(); 
     } 
    }); 
} 

Runnable mStatusChecker = new Runnable() { 
    @Override 
    public void run() { 

     updateStatus(); // this function can change value of mInterval. 
     mHandler.postDelayed(mStatusChecker, mInterval); 

    } 
}; 

void startRepeatingTask() { 
    mStatusChecker.run(); 
    mStarted = true; 
} 

void stopRepeatingTask() { 
    mHandler.removeCallbacks(mStatusChecker); 
    mStarted = false; 
} 
+0

對不起,我正在擴展FragmentActivity – user4799681

+0

您能否給我更多的信息? 是否要僅在Fragment中顯示AlertDialog? –

+0

你的意思是通過避免重複使用相同的代碼來在兩個片段中顯示對話框? –

回答

0

試試這個代碼,

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends FragmentActivity { 

    Button dfragbutton; 
    Button alertdfragbutton; 
    FragmentManager fm = getSupportFragmentManager(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from activity_main.xml 
     setContentView(R.layout.activity_main); 

     // Locate the button in activity_main.xml 
     dfragbutton = (Button) findViewById(R.id.dfragbutton); 
     alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton); 

     // Capture button clicks 
     dfragbutton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       DFragment dFragment = new DFragment(); 
       // Show DialogFragment 
       dFragment.show(fm, "Dialog Fragment"); 
      } 
     }); 

     // Capture button clicks 
     alertdfragbutton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       AlertDFragment alertdFragment = new AlertDFragment(); 
       // Show Alert DialogFragment 
       alertdFragment.show(fm, "Alert Dialog Fragment"); 
      } 
     }); 
    } 
} 
0

您可以使用DialogFragment來顯示警報DialogFragment有一個方法onCreateDialog中,你可以寫你彈出式警報邏輯

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 

     mLayoutInflater = LayoutInflater.from(mContext); 

     rootView = mLayoutInflater.inflate(R.layout.fragment_info_dialog, null, false); 
    alertDialog = builder.create(); 
     alertDialog.setCancelable(false); 
     alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

     return alertDialog; 
    } 
0

你可以輕鬆做到這一點與DialogFragment。只要閱讀文檔,他們也會給出如何在應用程序中實現的示例。如果你想要一個很好的例子,你可以按照這個Android DialogFragment Tutorial

這裏是代碼做

public static class MyAlertDialogFragment extends DialogFragment { 

public static MyAlertDialogFragment newInstance(int title) { 
    MyAlertDialogFragment frag = new MyAlertDialogFragment(); 
    Bundle args = new Bundle(); 
    args.putInt("title", title); 
    frag.setArguments(args); 
    return frag; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    int title = getArguments().getInt("title"); 

    return new AlertDialog.Builder(getActivity()) 
      .setIcon(R.drawable.alert_dialog_icon) 
      .setTitle(title) 
      .setPositiveButton(R.string.alert_dialog_ok, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         ((FragmentAlertDialog)getActivity()).doPositiveClick(); 
        } 
       } 
      ) 
      .setNegativeButton(R.string.alert_dialog_cancel, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         ((FragmentAlertDialog)getActivity()).doNegativeClick(); 
        } 
       } 
      ) 
      .create(); 
    } 
} 

製作的方法和要顯示的警告對話框中調用這個方法,在您的活動。

void showMyDialog() { 
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
     R.string.alert_dialog_two_buttons_title); 
    newFragment.show(getFragmentManager(), "dialog"); 
} 

快樂的編碼歡呼聲。