I saw a question “loading…” above the technical q&A. The last three points are dynamic, so an effect is implemented. I think about it, but I can’t think of a good way to deal with it.
Tried it and did it in the stupidest way possible. Let’s take a look at the results:
This is done by customizing a Dialog. The loading effect is implemented inside the Dialog, and the progress is controlled from within the Activity.
Here is the Dialog implementation class:
public class CustomDialog extends AlertDialog { public CustomDialog(Context context) { super(context); } private TextView tv_loading; private ProgressBar progressBar; private Timer timer; private int count = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_progress); tv_loading = (TextView) findViewById(R.id.tv_loading); progressBar = (ProgressBar) findViewById(R.id.pb); Display d = getWindow().getWindowManager().getDefaultDisplay(); WindowManager.LayoutParams lp = getWindow().getAttributes(); Lp.width = (int) (d.getwidth () * 0.8); getWindow().setAttributes(lp); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0); }}, 300, 300); setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (timer ! = null) { timer.cancel(); }}}); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { count++; if (count > 3) { count = 1; } switch (count) {case 1: tv_load.settext (" loading. "); break; Case 2: tv_load.settext (" loading... ") ); break; Case 3: tv_loading.setText(" loading...") ); break; }}}; public void setProgress(int progress) { progressBar.setProgress(progress); if (progress == 100) { this.dismiss(); }}}Copy the code
The layout files are a TextView, a ProgressBar, and dialog_progress.xml
Copy the code
Because I didn’t think of any other ideas, I could only change the display of TextView through Timer. The ProgressBar (ProgressBar) is a custom horizontal ProgressBar on Android that has rounded corners. Dialog is like this. The next call is mainActivity.class
public class MainActivity extends FragmentActivity { private CustomDialog customDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); customDialog = new CustomDialog(this); } private int count = 0; public void tvClick(View view) { customDialog.show(); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { count += 10; runOnUiThread(new Runnable() { @Override public void run() { if (customDialog ! = null && customDialog.isShowing()) { customDialog.setProgress(count); }}}); if (count >= 100) { timer.cancel(); }}}, 0, 500); customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (timer ! = null) timer.cancel(); count = 0; }}); }}Copy the code
Here, the Timer is also used to simulate the loading progress. (In the process of writing, I feel that the Timer operation is much more convenient than the other two methods.) Click events I call directly in XML.
Copy the code
In addition, this type of click event writing is not available in fragments and can only be triggered by setOnClickListener.
Code is not much, as a casual note.
Download the Demo