簡體   English   中英

Android-從對話框中選擇后如何使可繪制的圖像顯示在圖像視圖中

[英]Android - how to make drawable show in image view after selected from dialog

我正在制作類似於說話湯姆的游戲應用程序。 在開始新游戲時我選擇了角色性別后,它應該在所有其他活動中顯示所選的圖像(在ImageView中)。 我設法只在主屏幕上執行此操作,因此我需要一個如何使它在所有其他屏幕上可見的想法。 這是代碼:

主要活動:

public class MainActivity extends Activity implements OnClickListener {

    Button bNew;
    Button bContinue;
    Button bFemale;
    Button bMale;
    Context context = this;
    View view = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        initListeners();
    }

    private void initialize() {
        bNew = (Button) findViewById(R.id.bNew);
        bContinue = (Button) findViewById(R.id.bContinue);

    }

    private void initListeners() {
        bNew.setOnClickListener(this);
        bContinue.setOnClickListener(this);
    }

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.bNew:
            Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
            dialog.setContentView(R.layout.activity_dialog);
            Button bFemale = (Button) dialog.findViewById(R.id.bFemale);
            bFemale.setBackgroundResource(R.drawable.bfemale);
            Button bMale = (Button) dialog.findViewById(R.id.bMale);
            bMale.setBackgroundResource(R.drawable.bmale);
            bFemale.setOnClickListener(this);
            bMale.setOnClickListener(this);
            dialog.show();

            bFemale.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startLiving(R.drawable.bfemale);
                }

            });
            bMale.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startLiving(R.drawable.bmale);
                }
            });
            break;

        case R.id.bContinue:
            startLiving();
            break;
        }

    }


    public void startLiving() {
        Intent i = new Intent();
        i.setClass(this, LivingActivity.class);
        startActivity(i);
    }

    public void startLiving(int gender) {
        Intent i = new Intent();  
        i.setClass(this, LivingActivity.class);
        i.putExtra("gender", gender);
        startActivity(i);
    }
}

生活活動:

public class LivingActivity extends Activity implements OnClickListener {

    Button bBack;
    Button bKitchen;
    Button bBathroom;
    Button bBedroom;
    Button bGarden;
    ImageView daisy;
    ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_living);

        initialize();
        initListeners();
    }

    private void initialize() {
        this.bBack = (Button) findViewById(R.id.bBack);
        this.bKitchen = (Button) findViewById(R.id.bKitchen);
        this.bBathroom = (Button) findViewById(R.id.bBathroom);
        this.bBedroom = (Button) findViewById(R.id.bBedroom);
        this.bGarden = (Button) findViewById(R.id.bGarden);

        daisy = (ImageView) findViewById(R.id.ivImage);
        daisy.setImageResource(getIntent().getIntExtra("gender", 0));
        daisy.setDrawingCacheEnabled(false);

    }

    private void initListeners() {
        bBack.setOnClickListener(this);
        bKitchen.setOnClickListener(this);
        bBathroom.setOnClickListener(this);
        bBedroom.setOnClickListener(this);
        bGarden.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.bBack:
            startMain();
            break;
        case R.id.bKitchen:
            startKitchen();
            break;
        case R.id.bBathroom:
            startBathroom();
            break;
        case R.id.bBedroom:
            startBedroom();
            break;
        case R.id.bGarden:
            startGarden();
            break;
        }
    }

    // menu
    private void startMain() {
        Intent i = new Intent();
        i.setClass(this, MainActivity.class);
        startActivity(i);
    }

    private void startKitchen() {
        Intent i = new Intent();
        i.setClass(this, KitchenActivity.class);
        startActivity(i);
    }

    private void startBathroom() {
        Intent i = new Intent();
        i.setClass(this, BathroomActivity.class);
        startActivity(i);
    }

    private void startBedroom() {
        Intent i = new Intent();
        i.setClass(this, BedroomActivity.class);
        startActivity(i);
    }

    private void startGarden() {
        Intent i = new Intent();
        i.setClass(this, GardenActivity.class);
        startActivity(i);
    }
}

您可以使用其中任何一個

 1. specify the drawable as a public static variable.

 2. Pass the drawable name as an intent extra and get the corresponding image whereever you want them.

//pass the image name
Intent i = new Intent(this, LivingActivity.class);  
i.putExtra("gender", gender);
startActivity(i);

Get corresponding image in onCReate() of LivingActivity

getResources().getDrawable(this.getIntent().getStringExtra("gender"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM