2011-09-29 32 views
-4

就在我以为我有这个收藏的时候,我意识到我只能使用活动中的按钮按照它们写在Activity.java中的相同顺序例如:不能执行按钮5按下,直到1,2,3和4全部按下!) 我该如何解决这个问题?7个按钮,3个AsyncTasks,1个活动,还有一个噩梦

Activity.Java:

public class TwoActivity extends Activity { 

ProgressBar progressBar; 
TextView textView41; 
TextView textView42; 
TextView textView43; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.two); 

    progressBar = (ProgressBar)findViewById(R.id.progressbar_Horizontal); 
    progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.blue_progress)); 
    textView41 = (TextView)findViewById(R.id.dl41text); 
    textView42 = (TextView)findViewById(R.id.dl42text); 
    textView43 = (TextView)findViewById(R.id.dl43text); 


    Button button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startDownload(); 

     } 
    }); 
} 
private void startDownload() { 
    String url = "http://dl.dropbox.com/u/43058382/steelers1.jpg"; 
    new DownloadFileAsync().execute(url); 
} 

class DownloadFileAsync extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    progressBar.setProgress(0); 
    progressBar.setVisibility(View.VISIBLE); 
    textView41.setVisibility(View.VISIBLE); 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... aurl) { 
    int count; 

try { 

URL url = new URL(aurl[0]); 
URLConnection conexion = url.openConnection(); 
conexion.connect(); 

int lenghtOfFile = conexion.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

InputStream input = new BufferedInputStream(url.openStream()); 
OutputStream output = new FileOutputStream("/sdcard/steelers1.jpg"); 

byte data[] = new byte[1024]; 

long total = 0; 

    while ((count = input.read(data)) != -1) { 
     total += count; 
     publishProgress(""+(int)((total*100)/lenghtOfFile)); 
     output.write(data, 0, count); 
    } 

    output.flush(); 
    output.close(); 
    input.close(); 
} catch (Exception e) {} 
return null; 

} 
protected void onProgressUpdate(String... progress) { 
    progressBar.setProgress(Integer.parseInt(progress[0])); 
} 

@Override 
protected void onPostExecute(String unused) { 
    progressBar.setVisibility(View.INVISIBLE); 
    textView41.setVisibility(View.INVISIBLE); 


    Button button2 = (Button) findViewById(R.id.button2); 
    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View w) { 
      startDownload(); 

     } 
    }); 
} 
private void startDownload() { 
    String url = "http://dl.dropbox.com/u/43058382/steelers2.jpg"; 
    new DownloadFileAsync2().execute(url); 
} 

class DownloadFileAsync2 extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    progressBar.setProgress(0); 
    progressBar.setVisibility(View.VISIBLE); 
    textView42.setVisibility(View.VISIBLE); 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... aurl) { 
    int count; 

try { 

URL url = new URL(aurl[0]); 
URLConnection conexion = url.openConnection(); 
conexion.connect(); 

int lenghtOfFile = conexion.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

InputStream input = new BufferedInputStream(url.openStream()); 
OutputStream output = new FileOutputStream("/sdcard/steelers2.jpg"); 

byte data[] = new byte[1024]; 

long total = 0; 

    while ((count = input.read(data)) != -1) { 
     total += count; 
     publishProgress(""+(int)((total*100)/lenghtOfFile)); 
     output.write(data, 0, count); 
    } 

    output.flush(); 
    output.close(); 
    input.close(); 
} catch (Exception e) {} 
return null; 

} 
protected void onProgressUpdate(String... progress) { 
    progressBar.setProgress(Integer.parseInt(progress[0])); 
} 

@Override 
protected void onPostExecute(String unused) { 
    progressBar.setVisibility(View.INVISIBLE); 
    textView42.setVisibility(View.INVISIBLE); 

    Button button3 = (Button) findViewById(R.id.button3); 
    button3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View w) { 
      startDownload(); 

     } 
    }); 
} 
private void startDownload() { 
    String url = "http://dl.dropbox.com/u/43058382/steelers3.jpg"; 
    new DownloadFileAsync3().execute(url); 
} 

class DownloadFileAsync3 extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    progressBar.setProgress(0); 
    progressBar.setVisibility(View.VISIBLE); 
    textView43.setVisibility(View.VISIBLE); 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... aurl) { 
    int count; 

try { 

URL url = new URL(aurl[0]); 
URLConnection conexion = url.openConnection(); 
conexion.connect(); 

int lenghtOfFile = conexion.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

InputStream input = new BufferedInputStream(url.openStream()); 
OutputStream output = new FileOutputStream("/sdcard/steelers3.jpg"); 

byte data[] = new byte[1024]; 

long total = 0; 

    while ((count = input.read(data)) != -1) { 
     total += count; 
     publishProgress(""+(int)((total*100)/lenghtOfFile)); 
     output.write(data, 0, count); 
    } 

    output.flush(); 
    output.close(); 
    input.close(); 
} catch (Exception e) {} 
return null; 

} 
protected void onProgressUpdate(String... progress) { 
    progressBar.setProgress(Integer.parseInt(progress[0])); 
} 

@Override 
protected void onPostExecute(String unused) { 
    progressBar.setVisibility(View.INVISIBLE); 
    textView43.setVisibility(View.INVISIBLE); 

    Button button16 = (Button) findViewById(R.id.button16); 
    button16.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Intent myIntent = new 
        Intent(TwoActivity.this, ModdifyMyWiiActivity.class); 
      TwoActivity.this.startActivity(myIntent); 
     } 
    }); 

    Button button18 = (Button) findViewById(R.id.button18); 
    button18.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Intent myIntent = new 
        Intent(TwoActivity.this, ThreeActivity.class); 
      TwoActivity.this.startActivity(myIntent); 
     } 
    }); 

    Button button19 = (Button) findViewById(R.id.button19); 
    button19.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Intent myIntent = new 
        Intent(TwoActivity.this, FourActivity.class); 
      TwoActivity.this.startActivity(myIntent); 
     } 
    }); 
} 


    public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu, menu); 
      return true; 

    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.menuexit: 
      final Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
      break; 
     case R.id.menucontact: 
      final Intent intent1 = new Intent(android.content.Intent.ACTION_SEND); 
      intent1.setType("text/plain"); 
      intent1.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
      intent1.putExtra(android.content.Intent.EXTRA_SUBJECT, "Transform Mii Help"); 
      final PackageManager pm = getPackageManager(); 
      final List<ResolveInfo> matches = pm.queryIntentActivities(intent1, 0); 
      ResolveInfo best = null; 
      for (final ResolveInfo info : matches) 
       if (info.activityInfo.packageName.endsWith(".gm") || 
        info.activityInfo.name.toLowerCase().contains("gmail")) best = info; 
      if (best != null) 
       intent1.setClassName(best.activityInfo.packageName, best.activityInfo.name); 
      startActivity(intent1); 

      break; 
     } 
     return true; 

    } 
} 
} 
} 
} 

而且她是layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main_bg_hdpi" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:weightSum="1"> 
    <ScrollView android:id="@+id/scrollView1" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="390dp"> 
     <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:orientation="vertical" android:weightSum="1" android:layout_height="wrap_content"> 
      <Button android:src="@drawable/fourone_files" android:background="@drawable/modfiles41" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="22dp"></Button> 
      <Button android:src="@drawable/fourtwo_files" android:background="@drawable/modfiles42" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_alignTop="@+id/button1" android:layout_centerHorizontal="true"></Button> 
      <Button android:src="@drawable/fourthree_files" android:background="@drawable/modfiles43" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:layout_alignTop="@+id/button2" android:layout_alignParentRight="true"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button4" android:layout_below="@+id/button3" android:layout_alignParentLeft="true" android:layout_marginTop="22dp"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button5" android:layout_alignBottom="@+id/button4" android:layout_centerHorizontal="true"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button6" android:layout_alignBottom="@+id/button5" android:layout_alignParentRight="true"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button7" android:layout_below="@+id/button4" android:layout_alignParentLeft="true" android:layout_marginTop="22dp"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button8" android:layout_alignBottom="@+id/button7" android:layout_alignLeft="@+id/button5"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button9" android:layout_alignBottom="@+id/button8" android:layout_alignParentRight="true"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button10" android:layout_below="@+id/button7" android:layout_alignParentLeft="true" android:layout_marginTop="22dp"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button11" android:layout_alignBottom="@+id/button10" android:layout_alignLeft="@+id/button8"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button12" android:layout_alignTop="@+id/button11" android:layout_alignParentRight="true"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button13" android:layout_below="@+id/button10" android:layout_alignParentLeft="true" android:layout_marginTop="22dp"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button14" android:layout_alignBottom="@+id/button13" android:layout_alignLeft="@+id/button2"></Button> 
      <Button android:background="@drawable/unused" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button15" android:layout_alignBottom="@+id/button14" android:layout_alignParentRight="true"></Button> 
      <TextView android:text="** Get your System Menu Version by accessing the Wii Options Menu " android:id="@+id/CustomFontText" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:paddingLeft="5dip" android:paddingRight="5dip" android:focusable="true" android:focusableInTouchMode="true" android:freezesText="true" android:textColor="@color/wiiblue" android:textStyle="bold" android:layout_below="@+id/button15" android:layout_marginTop="22dp"></TextView> 
      <ProgressBar android:layout_width="fill_parent" android:layout_height="35dp" style="?android:attr/progressBarStyleHorizontal" android:id="@+id/progressbar_Horizontal" android:max="100" android:layout_below="@+id/button15" android:layout_marginTop="15dp" android:visibility="invisible" ></ProgressBar> 
      <TextView android:id="@+id/dl41text" android:text="Downloading 4.1 Mod Pack..." android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" android:textStyle="bold" android:gravity="center" android:visibility="invisible" android:layout_below="@+id/button15" android:layout_marginTop="22dp"></TextView> 
      <TextView android:id="@+id/dl42text" android:text="Downloading 4.2 Mod Pack..." android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" android:textStyle="bold" android:gravity="center" android:visibility="invisible" android:layout_below="@+id/button15" android:layout_marginTop="22dp"></TextView> 
      <TextView android:id="@+id/dl43text" android:text="Downloading 4.3 Mod Pack..." android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" android:textStyle="bold" android:gravity="center" android:visibility="invisible" android:layout_below="@+id/button15" android:layout_marginTop="22dp"></TextView> 

     </RelativeLayout> 

    </ScrollView> 

/>

菜单没有任何显示!所有帮助赞赏!

+2

两个长码片断,一个难以理解的问题描述,你的问题! ;-) – Knickedi

回答

2

为什么不创建一个可以做4种不同类型下载的异步类?这个类将作为参数:按钮,textview和url。那么你有四分之一的代码。在异步中,使所有按钮不可点击(setClickable(false))。

无论如何,您需要为OnCreate()中的所有按钮分配一个onclicklistener。您可以创建自定义类并切换按钮类型,或者仅执行个性化的匿名类路由。

+0

林猜测我将不得不使异步一个单独的类,然后让它按钮上按下按钮。但是,如何实现四种不同的下载? “如果”陈述? – jb15613

+0

您目前已拥有支持多个网址的功能。当你在async任务上调用execute()时,你会传递一个url。只需更改此网址每个按钮。 – Ian

4

您需要重写onCreateOptionsMenu方法。我给你的示例代码:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(group1Id,searchBtnId ,searchBtnId,"Search"); 
    menu.add(group2Id,scheduleBtnId ,scheduleBtnId,R.string.schedule); 
    menu.add(group2Id,playBtnId ,playBtnId,"Play"); 
    menu.add(group2Id,stopBtnId ,stopBtnId,R.string.stop); 
    return super.onCreateOptionsMenu(menu); 
    }