public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(MainActivity.this, "abcdefg", Toast.LENGTH_LONG); showToast(toast, 10); // 显示10秒 } }); } /** * * @param toast * @param second */ private void showToast(final Toast toast, final int second) { new Thread(new Runnable() { int tempSecond = 0; @Override public void run() { while (tempSecond < second) { toast.show(); try { Thread.sleep(1000); } catch (InterruptedException e) { } tempSecond++; } toast.cancel(); } }).start(); }
转: