如何传递整数?

时间:2019-02-14 13:13:16

标签: android android-intent menu integer boolean

如何将整数传递给public void方法,以便可以将字符串传递给其他活动?我希望将int数传递给公共void openActivityOne()方法,但我无法正确处理

public void openActivityOne(){
    Intent intent = new Intent(this, ActivityOne.class);
    String strName = "4";
    intent.putExtra("magazine", strName);
    startActivity(intent);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()){
        case R.id.item1:
            Toast.makeText(this, "Galerij geselecteerd", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.magazine:
            Toast.makeText(this, "item 2 geselecteerd", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.magazine1:
            int nummer = 1;
            openActivityOne();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}

1 个答案:

答案 0 :(得分:0)

openActivityOne()处设置整数参数:

   public void openActivityOne(int n){
    Intent intent = new Intent(this, ActivityOne.class);
    // do what you want with n like
    intent.putExtra("something", n);
    String strName = "4";
    intent.putExtra("magazine", strName);
    startActivity(intent);
  }

并致电:

case R.id.magazine1:
    int nummer = 1;
    openActivityOne(nummer);
    return true;