控制toast出现位置以及progressbar的出现消失

控制toast出现位置:

import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; /** * Class which shows how to show toast * @author FaYnaSoft Labs */ public class Main extends Activity { private Button clickBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clickBtn = (Button) findViewById(R.id.click); clickBtn.setText("Show message"); clickBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast msg = Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG); msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2); msg.show(); } }); } }

 

progressbar的出现消失:

import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Class which shows how to work with handler class * @author FaYnaSoft Labs */ public class Main extends Activity { private Button clickBtn; private ProgressDialog progressDialog; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); progressDialog.dismiss(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); clickBtn = (Button) findViewById(R.id.click); clickBtn.setText("Click me"); clickBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { processThread(); } }); } private void processThread() { progressDialog = ProgressDialog.show(Main.this, "", "Doing..."); new Thread() { public void run() { longTimeMethod(); handler.sendEmptyMessage(0); } }.start(); } private void longTimeMethod() { try { Thread.sleep(10000); } catch (InterruptedException e) { Log.e("tag", e.getMessage()); } } }

你可能感兴趣的:(Android知识杂记)