2016-04-09 10 views
0

私はこの質問に答えることを避け、自分自身でそれを理解したかったのですが、午後全体が無駄になった後、ここにいます。私は私のアプリが起動するときに3つのボタンをロードしようとしています。私は複数のstartActivitiesを使用しようとしましたが、一度にロードするのは1つだけでした。理由はわかりません。私もAsyncTasksを使ってみましたが、私がしようとしていたものに対してはあまりにも複雑でした。たとえば、ボタンの1つがGoogleマップアプリケーションを開くことになっていました。私はすでにコードを持っていて、それが効いていましたが、私はそれを行うボタンと別のことをする2つのボタンを必要としています。複数のボタンを同時にロードするにはどうすればよいですか?

答えて

0

あなたはこのようAsyncTaskを使用する必要があるとしている:

ボタンをクリックすると、それはAsyncTaskを実行します:

public void ThirtySecVideoPlayer(){ 
    ThirtySecondImageButton =  (ImageButton)findViewById(R.id.ThirtySecAdImageButton); 
    ThirtySecondImageButton.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        String Category = "Cleaninig"; 

        String ProductUploadMethod = "ProductUploadMethod"; 
        ProductUpload ProductUpload = new ProductUpload(); 
        ProductUpload.execute(ProductUploadMethod, Category); 
       } 
      } 
    ); 

} 

次に、あなたのAsyncTaskにあなたがのparamsを取得し、私たちのことAsyncTaskで:

private class ProductUpload extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 

    } 

    @Override 
    protected String doInBackground(String... params) { 

     String Category = params[1]; 

    } 

     @Override 
     protected void onPostExecute (String result){ 

      } 
     } 

     @Override 
     protected void onProgressUpdate (Void...values){ 
     } 
} 

あなたは、各ボタンにボタンのコードを使用して、同じAsyncTaskを呼び出していますが、確認する必要がありますすることができ、その変数のカテゴリの実行If文を使ったDoInBackroundの正しいコード!

0

ボタンをonCreateメソッドで動的に作成することができます。

// this is the text to put in the created button 
String[] btnText = ["Button 1", "Button 2","Button 3"]; 
// the ID allows to determine witch button was pressed 
int btnID = 0; 
// in the for web be created the 3 button dynamically 
for(int i = 0; i<3; i++){ 
final Button Btn = new Button(this); 
Btn.setText(btnText[]); 

//This is to give the button the size he need to wrap the text in it 
LinearLayout.LayoutParams LayoutParams = new LinearLayout.LayoutParams(LayoutParams.wrap_content, LayoutParams.wrap_content); 
Btn.setLayoutParams(LayoutParams); 

Btn.setId(btnID); 

btnID ++; 

Btn.setOnClickListener(new OnClickListener(){ 

//This returns the Id of the clicked button 
// the Id of each button was set by the "Btn.setId(btnID);" 
DetectclickedButton(Btn.getId()); 
    }); 
} 

public void DetectclickedButton(id){ 

switch(id){ 
case 1: 
// Do something in the click of the 1 button 
break; 
case 2: 
// Do something in the click of the 2 button 
break; 
case 3: 
// Do something in the click of the 3 button 
break; 
} 

} 
+0

このスレッドに投稿したばかりのものを教えてもらえますか? – deeup511

関連する問題