具有来自另一个类的内部类的变量

时间:2019-02-07 23:38:03

标签: java android android-studio

我目前正在使用10x10网格,需要更改选定的框或带有其邻居的框。为了做到这一点,我认为我需要一个弹出窗口(设置更改)。我没有使弹出窗口出现的按钮,我只需要相应的坐标即可。 我已经尝试过此代码,但是在私有void viewClick()方法中,变量设置为0。 在此之前的一切都很好。如何保留变量的值?

MainActivity:

View gView;
GameCoords mGame;
private int xx, yy;

public void setCol(int column){
    this.yy = column;
    System.out.println(" y : " + xx + yy);  //fine
}
public void setRow(int row){
    this.xx = row;
    System.out.println("x  : " + xx );  //fine
}

public int getRow(){
    return xx;
}
public int getCol(){
    return yy;
}

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

    mGame = new GameCoords ();

    gView = findViewById(R.id.view);
    gView.setOnClickListener(new ViewClick());
}

private void viewClick(){
    System.out.println(" On create sfter ships : " + xx + yy);  //both are 0
    LayoutInflater inflater = (LayoutInflater) MainActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    PopupWindow popupWindow = new PopupWindow(inflater.inflate(
            R.layout.select_ship, null, false), 1000, 700, true);

    popupWindow.showAtLocation(findViewById(R.id.view),
            Gravity.CENTER, 0, 0);

    Toast.makeText(getApplicationContext(), "X + Y" + xx + yy  ,Toast.LENGTH_LONG).show();
}

public class ViewClick implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        viewClick();
    }
}

GameCoords(获取变量的位置):

public int clickedCoords(int column, int row) {

    System.out.println("Game class " + row + column);   //received fine

    mainActivity.setRow(row);
    mainActivity.setCol(column);
    return 0;
}

所以,我应该怎么做才能在xx和yy的viewClick方法中包含它们的值。 我想挑战自己,做一个轻松的任务,但事实并非如此。任何线索,提示或代码段都会受到赞赏。

我也尝试使用吸气剂,但它们也不起作用。 编辑2:当我在没有获取/设置的情况下将xx声明为12(private int xx = 12;)时,它在内部类中运行良好。我的代码在从其他一些类获取数字时遇到了问题

0 个答案:

没有答案
相关问题