0

パーセンテージを表示する必要のないプログレスバーを作成しようとしています。

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
        case R.id.back:
            this.dispatchKeyEvent( new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ) );
            finish();
            return true;
        case R.id.read:
            // This is where I would start my spinner....
            if( myService.getState() != 3 ) {
                myService.connect( MainMenu.previousDevice, true );
            }
            readWaves();
            return true;
        default:
            return super.onContextItemSelected( item );
    }
}

スピナーはreadWaves()関数で停止します。

private void readWaves() {
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_V );
        // Turn the Spinner off Here.
        doOtherThings();
    }

スレッドを使用してプログレスバーを使用する方法、xmlファイルにプログレスバーを追加する方法、およびその他の多くの方法を教えてくれるコードがたくさん見つかりました。

私にとって最善の方法は、CustomDialogクラスを作成してポップアップし、終了するまでスピナーを回転させることだと思います。

ここに示されている最初の答えは、私が欲しいものを見つけるのにはるかに近いものですが、残念ながら彼の解決策は機能しません。それが機能しない理由は、コードの最後のセクションが原因です。

public MyProgressDialog(Context context) {
    super(context, R.style.NewDialog);
}

Android 2.3を使用していますが、のstyle属性はありませんR。自分で追加しようとするとエラーが発生します。誰かがこれを修正する方法を知っていますか?

編集:

これは私が使ってみたコードですProgressDialog

case R.id.read:
            pDialog = new ProgressDialog( Waves.this );
            pDialog.setTitle( "Waves" );
            pDialog.setMessage( "Reading..." );
            pDialog.show();
            if( myService.getState() != 3 ) {
                myService.connect( MainMenu.previousDevice, true );
            }
            readWaves();
            return true;
        default:
            return super.onContextItemSelected( item );
    }
}

private void readWaves() {
    if( spinnerChoice == 0 ) {
        Log.i( "IN IF", "IN IF" );
        // Diff Voltage
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_V );
    } else {
        // Current 
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_I );
    }
    Log.i( "READ WAVES", "After If/Else" );
    pDialog.dismiss();
    /*
4

1 に答える 1

1

この特定の目的のために作成されたダイアログが既に存在します。ProgressDialogをチェックしてください。次のように使用できます。

//show the dialog
ProgressDialog pd = new ProgressDialog(YourActivity.this);
pd.setTitle("Your title");
pd.setMessage("Helpful message to the user");
pd.show();

/**************
* Do some stuff
**************/

//Hide the dialog
pd.dismiss();

編集: これは、オプション メニューでどのように表示されるかの小さなサンプルです。

public boolean onCreateOptionsMenu(Menu menu) {  
    menu.add(0, 42, 0, "Option"); 
    return true;  
}  
public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {  
    case 42: 
           ProgressDialog pd = new ProgressDialog(this);
           pd.show();
        break;
    default:
        //Do nothing      
    }  
    return false;  
}
于 2012-12-14T15:37:10.637 に答える