片段中的runOnUiThread()方法

时间:2015-08-05 22:00:36

标签: android android-fragments

我可以在片段中使用runOnUiThread吗?如何在片段中做到这一点?

 MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this,"any mesage",Toast.LENGTH_LONG).show();
        }
    });

2 个答案:

答案 0 :(得分:4)

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });

答案 1 :(得分:4)

使用getActivity()代替MainActivity.this。同时对Toast.makeText()方法

使用getApplicationContext()
getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getActivity().getApplicationContext(),"any mesage",Toast.LENGTH_LONG).show();
        }
    });