0

我正在开发猜测游戏应用程序,我的问题是,我创建了网格视图,用户在其中选择一个图像并输入其名称,如果正确,则它必须将其状态存储到文件中,并且网格视图状态中的特定图像具有要改变如何做到这一点......

第一个java文件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.andy);

    gv=(GridView)findViewById(R.id.gridView1);
    gv.setAdapter(new ImageAdapter(this,mImgs));


    gv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            //String name=getResources().getResourceEntryName(mImgs[arg2]);
            //Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();


            Bundle basket=new Bundle();
            Intent i=new Intent(Andy.this,AndyImg.class);
            basket.putString("hintName", hintName[arg2]);
            basket.putString("solvedName", solvedName[arg2]);                   
            i.putExtra("index",arg2);
            i.putExtra("lvl", 1);
            i.putExtras(basket);

            startActivityForResult(i, 0);
        }

    });
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        //Bundle b=data.getExtras();
        //int i=b.getInt("int");
        //int c=gv.getCount();



    }
}
}

第二个java文件:

 public class AndyImg extends Activity implements OnClickListener {
Button check;
TextView textv, solvedDisplay, coins, hintClick, solvedClick;
EditText et;
ImageView iv, ad;
Bundle basket;
int indexOfImg, level;
String hintName, solvedName;
AlertDialog alert;
int val = 1,coi;
LinearLayout ll;

SharedPreferences coinData, levelStatus,savedLevel;

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

    intializevars();
    getDataFromPrev();
    checkState();

    hintClick.setOnClickListener(this);
    solvedClick.setOnClickListener(this);
    check.setOnClickListener(this);
    solvedDisplay.setOnClickListener(this);

}

public void intializevars() {
    textv = (TextView) findViewById(R.id.displaygame);
    // hintDisplay = (TextView) findViewById(R.id.hint);
    solvedDisplay = (TextView) findViewById(R.id.solvedText);
    et = (EditText) findViewById(R.id.editgame);
    iv = (ImageView) findViewById(R.id.masterImg);

    hintClick = (TextView) findViewById(R.id.hinttv);
    solvedClick = (TextView) findViewById(R.id.solvedtv);
    check = (Button) findViewById(R.id.checkgame);
    coins = (TextView) findViewById(R.id.coins);
    ll = (LinearLayout) findViewById(R.id.lin1);
}

public void getDataFromPrev() {
    basket = getIntent().getExtras();
    indexOfImg = basket.getInt("index");// get index of selected image
    hintName = basket.getString("hintName");// get hint from andy
    solvedName = basket.getString("solvedName");// get fully solved name
    // Bitmap img=(Bitmap)getIntent().getParcelableExtra("img");
    level = basket.getInt("lvl");
    switch (level) {
    case 1:
        iv.setImageResource(Andy.mImgs[indexOfImg]);// set Selected image to
                                // the image view
        break;

    case 2:
        iv.setImageResource(AndyLevelTwo.mImgs[indexOfImg]);

        break;
    case 3:
        iv.setImageResource(AndyLevelThree.mImgs[indexOfImg]);
        break;
    }
}

public void checkState() {

}

public void fullScreen() {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {


    case R.id.checkgame:
                        // check button
        String editTxt = et.getText().toString();
        solvedName = solvedName.replace(" ", "");
        editTxt = editTxt.replace(" ", "");// remove spaces and compare
        if (editTxt.compareToIgnoreCase(solvedName) == 0) {

            textv.setText("Correct ");
            textv.append(solvedName);
            //Intent i = new Intent();  i thought to do it onActivityResult is this correct?or is there any better sol
            //i.putExtra("int", indexOfImg);
            //setResult(RESULT_OK, i);

            finish();

            // hintClick.setEnabled(false);
            //ll.setEnabled(false);
        } else {
            textv.setText("incorrect");
            textv.append(editTxt);

            // textv.setText(level);
        }
        break;

    }
}
4

0 回答 0