有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    我假设你想在某个地方显示时间,但不想在布局上显示时间,那么你可以用祝酒词来做

    public class MainActivity extends Activity {
    
        Button button;
    
        Long currentTime;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentTime != null) {
                        Long pastTime = System.currentTimeMillis() - currentTime;
                        Toast.makeText(
                                getApplicationContext(), "Time: " + pastTime, Toast.LENGTH_SHORT)
                                .show();
                    }
                    currentTime = System.currentTimeMillis();
                }
            });
        }
    }
    

    阅读更多:http://developer.android.com/guide/topics/ui/notifiers/toasts.html

  2. # 2 楼答案

    我不明白:

    but I don't want the time to be displayed anywhere on the app.

    那就别展示了。很少有类像Time或Date(或my fav lib JodaTime)可以与时间一起工作,您可以轻松地以毫秒为单位计算时间间隔