如何在按钮点击时显示不同的下载图像

时间:2012-10-20 17:15:20

标签: android url imageview fragment

我想从URL下载不同的图像,并在单击btn1时显示其中一个图像,单击btn2时显示另一个图像。我怎么做?这是我到目前为止所尝试的:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Button;



    public class MainActivity extends FragmentActivity {
Fragment fragment;
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;


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



    btn1=(Button)findViewById(R.id.btn1);
    btn2=(Button)findViewById(R.id.btn2);
    btn3=(Button)findViewById(R.id.btn3);
    btn4=(Button)findViewById(R.id.btn4);
    btn5=(Button)findViewById(R.id.btn5);
    btn6=(Button)findViewById(R.id.btn6);
    btn7=(Button)findViewById(R.id.btn7);
    btn8=(Button)findViewById(R.id.btn8);
    btn9=(Button)findViewById(R.id.btn9);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    StartFragment myFragment = new StartFragment();
    ft.add(R.id.myFragment, myFragment);
    ft.commit();

    btn1.setOnClickListener(btnOnClickListener);
    btn2.setOnClickListener(btnOnClickListener);
    btn3.setOnClickListener(btnOnClickListener);
    btn4.setOnClickListener(btnOnClickListener);
    btn5.setOnClickListener(btnOnClickListener);
    btn6.setOnClickListener(btnOnClickListener);
    btn7.setOnClickListener(btnOnClickListener);
    btn8.setOnClickListener(btnOnClickListener);
    btn9.setOnClickListener(btnOnClickListener);
}



    Button.OnClickListener btnOnClickListener = new Button.OnClickListener()
{
    @Override
    public void onClick(View v) 
{


        Fragment newFragment;

        if (v == btn1){
            newFragment = new Fragment1();
        }else if (v == btn2){
            newFragment = new Fragment2();
        }else if (v == btn3){
            newFragment = new Fragment3();
        }else if (v == btn4){
            newFragment = new Fragment4();
        }else if (v == btn5){
            newFragment = new Fragment5();
        }else if (v == btn6){
            newFragment = new Fragment6();
        }else if (v == btn7){
            newFragment = new Fragment7();
        }else if (v == btn8){
            newFragment = new Fragment8();
        }else if (v == btn9){
            newFragment = new Fragment9();
        }else {
            newFragment = new StartFragment();
        }

        FragmentTransaction transaction =                                             getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.myFragment, newFragment);
        transaction.addToBackStack(null);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();




    }


    };




} 

1 个答案:

答案 0 :(得分:0)

在onClick中,对于每个单击按钮的情况,您可以调用此方法

位图bmImg;

void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData =new int[length];
byte[] bitmapData2 =new byte[length];
InputStream is = conn.getInputStream();

bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}