加载MainActivity时的进度条

时间:2018-01-24 09:10:16

标签: android user-interface progress-bar

我正在尝试在加载MainActivity时显示progresbar, 试过这个:

public class MainActivity extends Activity {
private ProgressBar spinner;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (ProgressBar)findViewById(R.id.progressBar1);
    spinner.setVisibility(View.VISIBLE);
    ...do stuff...
}
public void onStart() {
    super.onStart();
    openBT();
    spinner.setVisibility(View.GONE);
    ...do stuff...
}

}

问题:MainActivity空白约5秒钟,然后加载MainActivity。但它没有显示进度条。我如何修复?我的错是什么?谢谢!

.xml看起来像这样:

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />

整个OnCreate:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner = (ProgressBar)findViewById(R.id.progressBar1);
    spinner.setVisibility(View.VISIBLE);
    function1btn = (Button) findViewById(R.id.function1btn);
    ...
    btDataList = new ArrayList<>();

    mWaveLoadingView = (WaveLoadingView) findViewById(R.id.waveLoadingView);
    mWaveLoadingView.setShapeType(WaveLoadingView.ShapeType.CIRCLE);
    mWaveLoadingView.setCenterTitleColor(Color.DKGRAY);
    mWaveLoadingView.setCenterTitleStrokeColor(Color.LTGRAY);
    mWaveLoadingView.setCenterTitleStrokeWidth(2);
    mWaveLoadingView.setProgressValue(100);
    mWaveLoadingView.setBorderWidth(10);
    mWaveLoadingView.setAmplitudeRatio(20);
    mWaveLoadingView.setWaveColor(Color.argb(255,50,205,50)); // green -> 100%
    mWaveLoadingView.setBorderColor(Color.DKGRAY);
    mWaveLoadingView.setCenterTitle("No BT-Device connected");
    mWaveLoadingView.setAnimDuration(3000);
    mWaveLoadingView.pauseAnimation();
    mWaveLoadingView.resumeAnimation();
    mWaveLoadingView.cancelAnimation();
    mWaveLoadingView.startAnimation();

    final SharedPreferences mPrefsMaxCap = getSharedPreferences("label", 0);
    String mString = mPrefsMaxCap.getString("MaxCap", "0");
    maxEnergy = Integer.parseInt(mString);

    if (maxEnergy != 0){
        maxEnergyView.setText(mString);
    }

    function1btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        }
    });

    function2btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        }
    });

    function3btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        }
    });
}

openBT()方法:

private void openBT(){
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    checkBTState();
    Log.d(TAG, "...onStart - try connect...");
    // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    //   A MAC address, which we got above.
    //   A Service ID or UUID.  In this case we are using the
    //     UUID for SPP.

    try {
        btSocket = createBluetoothSocket(device);
    } catch (IOException e1) {
        errorExit("Fatal Error", "In onStart() and socket create failed: " + e1.getMessage() + ".");
    }

    // Discovery is resource intensive.  Make sure it isn't going on
    // when you attempt to connect and pass your message.
    btAdapter.cancelDiscovery();

    // Establish the connection.  This will block until it connects.
    Log.d(TAG, "...Connecting...");
    try {
        btSocket.connect();
        Log.d(TAG, "...Connection ok...");
    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
            errorExit("Fatal Error", "In onStart() and unable to close socket during connection failure" + e2.getMessage() + ".");
        }
    }

    // Create a data stream so we can talk to server.
    Log.d(TAG, "...Create Socket...");

    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        errorExit("Fatal Error", "In onStart() and output stream creation failed:" + e.getMessage() + ".");
    }
    mConnectedThread = new ConnectedThread(btSocket);
    mConnectedThread.start();
}

2 个答案:

答案 0 :(得分:0)

View.GONE方法中调用onResume(),如下所示..

@Override
    protected void onResume() {
        super.onResume();
        spinner.setVisibility(View.GONE);
    }

答案 1 :(得分:-1)

如果您在打开应用程序时遇到问题,请将此主题设置为启动器活动。

android:theme="@android:style/Theme.Translucent.NoTitleBar.F‌​ullscreen"

请参阅以下代码段。

    <application 
      your application name,icon,label etc.>

      <activity
        android:name="Your main launcher activity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>