2016-04-09 28 views
0

Bu soruya cevap vermekten kaçınmak ve kendim bulmaktan kaçınmak istedim, ama öğleden sonra bütün bir şey tükendikten sonra, buradayım. Uygulamam başladığında 3 düğmeyi yüklemeye çalışıyorum. Birden çok startActivities kullanmayı denedim, ancak bir seferde yalnızca bir tane yüklenecekti ve neden emin değilim. Ayrıca AsyncTasks kullanmayı denedim ama yapmaya çalıştığım şey için aşırı karmaşık görünüyorlardı. Örneğin, düğmelerden biri Google Haritalar uygulamasını açacaktı. Kodu zaten aldım ve bu işe sahiptim ama bunu yapan bir düğme istiyorum ve farklı şeyler yapan diğer iki düğme. Eğer AsyncTask çalıştırır düğmesini tıkladığındaBirden çok düğmeyi aynı anda nasıl yüklerim?

:

cevap

0

Böyle bir AsyncTask kullanmak zorunda gidiyoruz

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); 
       } 
      } 
    ); 

} 

Sonra AsyncTask size params almak ve bize onu AsyncTask içinde:

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){ 
     } 
} 

her düğme için düğme kodunu kullanmak ve daha sonra aynı AsyncTask diyoruz, ancak emin olmak gerekir ki değişken kategori yürüttüğü Bir If deyimi ile DoInBackround doğru kodu!

0

Düğmeyi dinamik olarak onCreate yönteminizde oluşturabilirsiniz.

// 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

Bu ileti dizisinde yeni gönderdiğim öğede bana yardımcı olabilir misiniz? – deeup511

İlgili konular