如何在android中调用添加的图像?

时间:2014-08-19 05:05:43

标签: android image android-layout view

我写了一些代码。我有4个图像要调用。但是,模拟器中只运行了三个图像。

其他图片不可见。

请让我知道为什么没有正确调用图像?

package com.mobility.visiontest.ui;

import com.mobility.visiontest.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class DuoChromeTest extends UIScreenActivity {

    private boolean isLeftEye = false;
    private boolean isRightEye = false;
    private int correctAnsCount = 0;
    private int tapCount = 0;
    private int testCount = 1;
    private int images[] = {R.drawable.d1,R.drawable.d2,R.drawable.d3,R.drawable.d4,R.drawable.dc1};
    private int dtest_image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duochrome_image);
        ImageView image = (ImageView) findViewById(dtest_image);
        Button button= (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() 
       {@Override
    public void onClick(View arg0) { 
           // TODO Auto-generated method stub 
           resetView();

       } });
    }



    public void resetView(){
        setContentView(R.layout.duochrometest);
        setObjects();
        int a[] = { R.id.leftEye, R.id.rightEye };
        setOnClickListener(a);

                }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.leftEye:
        case R.id.rightEye:
            resetTestView(R.layout.duochrome_layout);
            break;
        case R.id.option1:
        case R.id.option2:
        case R.id.option3:
        case R.id.option4:
            ++tapCount;
            if(v instanceof Button){
                Button button =(Button)v;
            if(button.getText().equals(getString(R.string.clearBoth) )||
                    button.getText().equals(getString(R.string.yes))){
                ++correctAnsCount;
            }
            }
            updateQuestionandAnswers();
            break;
        case R.id.testagain:
            ++testCount;
            resetView();
            break;
        case R.id.backtoTests:
            backToTests();
            break;
        default:
            break;
        }
    }



    private void resetTestView() {
        // TODO Auto-generated method stub

    }

    private void backToTests() {
        // TODO Auto-generated method stub
        Intent i = new Intent();
        i.setClassName(getApplicationContext(),getString(R.string.tests));
        startActivity(i);
        finish();

    }



    private void updateQuestionandAnswers() {
        // TODO Auto-generated method stub
        if(tapCount == 3){
//          TextView question = (TextView)findViewById(R.id.question);

            ((TextView)findViewById(R.id.question)).setText(R.string.duochromeQuestion2);

            ((Button)findViewById(R.id.option1)).setText(R.string.clearRed);
            ((Button)findViewById(R.id.option2)).setText(R.string.clearGreen);
            ((Button)findViewById(R.id.option3)).setText(R.string.Notvisible);

            ((Button)findViewById(R.id.option4)).setVisibility(View.INVISIBLE);
        }else if(tapCount > 3){
            showResult();
        }else{

            ((ImageView)findViewById(R.id.imageView)).setImageResource(images[tapCount]);
        }
    }

    private void showResult() {
        // TODO Auto-generated method stub
setContentView(R.layout.duochrometestresult);

        int a[] = { R.id.testagain, R.id.backtoTests};
        setOnClickListener(a);

        String result = null;
         if(correctAnsCount < 4 ){
            result = getString(R.string.duochromeResult2);
            if(testCount == 2 ){
                result = getString(R.string.duochromeResultIteration1);
                ((Button)findViewById(R.id.testagain)).setVisibility(View.INVISIBLE);
                testCount =1 ;
            }
        }else if( correctAnsCount == 3){
            result = getString(R.string.duochromeResult1);
            ((Button)findViewById(R.id.testagain)).setVisibility(View.INVISIBLE);
    }
         ((TextView)findViewById(R.id.atresult)).setText(result.replace("%d", String.valueOf(correctAnsCount)+"/3"));
            correctAnsCount = 0;
            tapCount = 0;
    }


    // Resets the vision acuity test view
        private void resetTestView(int layout) {
            setContentView(layout);
            ((ImageView)findViewById(R.id.imageView)).setImageResource(images[tapCount]);
            int a[] = { R.id.option1, R.id.option2, R.id.option3, R.id.option4 };
            setOnClickListener(a);
        }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void setObjects() {
        // TODO Auto-generated method stub
    }

    // Back to MainMenu.
    private void backToMain() {
        Intent i = new Intent();
        i.setClassName(getApplicationContext(), getString(R.string.tests));
        startActivity(i);
        finish();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        // super.onBackPressed();

        AlertDialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // Add the buttons
        builder.setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User clicked OK button
                        backToMain();
                    }
                });
        builder.setNegativeButton(R.string.no,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                        dialog.dismiss();
                    }
                });

        builder.setMessage(getString(R.string.exitTest));
        dialog = builder.create();
        dialog.show();

    }

}

1 个答案:

答案 0 :(得分:1)

我认为在if(tapCount == 3){条件下你没有使用

((ImageView)findViewById(R.id.imageView)).setImageResource(images[tapCount]);

现在它只会改变叮咬但不会显示图像