如何增加int变量的值?

时间:2015-05-07 07:43:33

标签: java android increment

每当用户点击一个按钮时,我想增加一个int变量的值,但是现在这个值只增加一次。

这是我用来增加变量p的值。

@Override
public void onClick(View v) {
    int p = 1;

    if (p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
    } else {
        p = p + 1;                      
        holder.textViewQuantity.setText("" + p);                        
    }
}

7 个答案:

答案 0 :(得分:4)

p位于方法内部时,它是局部变量,并且是每个方法调用的唯一值,每次都初始化为1

将它移动到类,它变成实例变量(AKA字段),并保持其最新值为类实例的生命周期。

阅读this doc on the 4 types of variables了解详情。

private int p = 1; //moved 

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

    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }

答案 1 :(得分:0)

变量p应该是类成员:

int p = 1;  

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


    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }

答案 2 :(得分:0)

将其声明为全局变量。让它对整个班级可见(我假设你不是一次又一次地声明对象)。目前,变量的范围仅限于函数,并且每次事件发生时此变量的值都为1.

每当调用此事件时,您的值p都被赋值为1。

class YourClass{

     int p;  /// creating p 
     YourClass(){
      p = 1;     /// initializing the value in constructor;
     } 


 // your onClick Event Code;
 @Override
 public void onClick(View v) {
 if(p == 9) {
 Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
   return ;
 }
 else {                              
   p = p+1;                        
    holder.textViewQuantity.setText(""+p);                      
 }
 }  

答案 3 :(得分:0)

将int p = 1移出函数,你将p重置为1 onclick。

答案 4 :(得分:0)

您的代码应如下所示:

private int p = 1;

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    if(p == 9) {
        Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
        return ;
    }
    else {                              
        p = p+1;                        
        holder.textViewQuantity.setText(""+p);                      
    }
}

int p应该是一个类成员。

答案 5 :(得分:0)

您可以将变量设为静态,然后计算给定类的所有按钮:

class Button {
   public static int p = 0;

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

   if(p == 9) {
       Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
       return ;
    }
    else {                              
       p = p+1;                        
       holder.textViewQuantity.setText(""+p);                      
    }
}

答案 6 :(得分:0)

private int p = 0;

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
     p+=1;
   if(p<9)
     holder.textViewQuantity.setText(String.valueof(p)); 
   else
    Toast.makeText(context, "You have reached to maximum number", Toast.LENGTH_LONG).show();
}
相关问题