如何通过android中的OnClickListener将递增的变量传递给intent

如何通过android中的OnClickListener将递增的变量传递给intent,android,android-emulator,Android,Android Emulator,我正在使用android应用程序,我有两个按钮。第一个按钮是活动模式,第二个按钮是非活动模式。如果我单击一次1按钮,意味着我需要激活2按钮,如果我点击1按钮2次,意味着2按钮处于非活动模式。我在onclick listener中使用i count variable创建树ID,然后根据我给定的条件的计数值增加该变量,但它不起作用,有人能建议我 public class Home extends Activity implements OnClickListener{ Button b1,b

我正在使用android应用程序,我有两个按钮。第一个按钮是活动模式,第二个按钮是非活动模式。如果我单击一次1按钮,意味着我需要激活2按钮,如果我点击1按钮2次,意味着2按钮处于非活动模式。我在onclick listener中使用i count variable创建树ID,然后根据我给定的条件的计数值增加该变量,但它不起作用,有人能建议我

    public class Home extends Activity implements OnClickListener{
Button b1,b2;
          int countk3;
        int countk4=0;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.home1);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b2.setEnabled(false);
    countk3 = getIntent().getIntExtra("k3", 0); 


    if(countk3==10 && countk4==1)
    {
           b2.setEnabled(true);
b2.setBackgroundResource(R.color.yellow);

    }
    else  if(countk3==10&&countk4!=1)
    {
         b2.setEnabled(false); 
b2.setBackgroundResource(R.color.red);
    }

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==b1)
    {
        countk4++;
    Log.i("value....",""+countk4);

        Intent i=new Intent(Home.this,GalleryActivity.class);
        i.putExtra("k", 1);
        i.putExtra("k1", 13);

        startActivity(i);
    }
    if(v==b2)
    {
        Intent i=new Intent(Home.this,GalleryActivity.class);
        i.putExtra("k", 2);
        i.putExtra("k1", 16);
        startActivity(i);
    }

}
   }

       public class GalleryActivity extends Activity implements
   AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
private TextSwitcher mSwitcher;
 int count=10;
private TextSwitcher mSwitcher1;
Gallery g;
static int nextbtn = 0;
static int prebtn = 0;
ImageAdapter iadapter;
int galpos;
  int countk1=0;
 int countk2;
public int ht;
    public int wt;
       String s;
          int l11;
  public static int motiongal = 3000;   
LinearLayout l1;
Integer length=0;
int length1;
ImageView i ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    ht = displaymetrics.heightPixels;
    wt = displaymetrics.widthPixels;
 l1=(LinearLayout)findViewById(R.id.linear);
    mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
    mSwitcher.setFactory(this);
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));
    mSwitcher.startLayoutAnimation();

    mSwitcher1 = (TextSwitcher) findViewById(R.id.switcher1);
    mSwitcher1.setFactory(this);
    mSwitcher1.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    mSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));
    mSwitcher1.startLayoutAnimation();
    try
    {
    g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setHorizontalScrollBarEnabled(false);
    Bundle bundle = getIntent().getExtras();
    galpos = bundle.getInt("GALPOS");
    g.setSelection(galpos);        
    g.setAnimationDuration(motiongal);
   countk1 = getIntent().getIntExtra("k", 0); 
   } catch(Exception e){
       }
    g.setOnItemSelectedListener(this);
    ImageButton nextButton = (ImageButton) findViewById(R.id.nextimage);
    nextButton.setOnClickListener(nextButtonOnClick);

    ImageButton preButton = (ImageButton) findViewById(R.id.preimage);
    preButton.setOnClickListener(preButtonOnClick);

}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    if(countk1==1)
    {
        mSwitcher.setText(mTextNames[position]);
    }
    if(countk1==2)
    {
         mSwitcher1.setText(mTextNames1[position]);
    }
   if(position==10)
   {
        Intent i=new Intent(GalleryActivity.this,Home.class);
       i.putExtra("k3", count);
       startActivityForResult(i, 0);
   }
    nextbtn = position;
}

public OnClickListener nextButtonOnClick = new OnClickListener(){
    public void onClick(View v) {           
        if(countk1==1)
        {
            mSwitcher1.setVisibility(View.INVISIBLE);
        if(nextbtn < (mTextNames.length-1))
        {     
            try
            {               
            mSwitcher.setText(mTextNames[nextbtn+1]);
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(nextbtn+1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);  

            }
            catch(Exception e)
            {}

        }else
        {
            try
            {               
            mSwitcher.setText(mTextNames[0]);   
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(0);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}
        }
        }

        if(countk1==2)
        {
            mSwitcher.setVisibility(View.INVISIBLE);
        if(nextbtn < (mTextNames1.length-1))
        {     
            try
            {               
            mSwitcher1.setText(mTextNames1[nextbtn+1]);
            mSwitcher1.startLayoutAnimation();              
            mSwitcher1.getDrawingTime();
            g.setSelection(nextbtn+1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);  

            }
            catch(Exception e)
            {}

        }else
        {
            try
            {               
            mSwitcher1.setText(mTextNames1[0]);   
            mSwitcher1.startLayoutAnimation();              
            mSwitcher1.getDrawingTime();
            g.setSelection(0);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}
        }
        }

      } 
};   

public OnClickListener preButtonOnClick = new OnClickListener(){
    public void onClick(View v) {           
            if(countk1==1)
            {
        if(nextbtn > 0)
        {
            try
            {               
        mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(nextbtn-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);

            }
            catch(Exception e)
            {}


        }else
        {
            try
            {               
            mSwitcher.setText(mTextNames[mTextNames.length-1]);
            mSwitcher.startLayoutAnimation();               
            mSwitcher.getDrawingTime();
            g.setSelection(mTextNames.length-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}

        }
            }


            if(countk1==2)
            {
        if(nextbtn > 0)
        {
            try
            {               
            mSwitcher1.startLayoutAnimation();              
            mSwitcher1.getDrawingTime();
            g.setSelection(nextbtn-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);

            }
            catch(Exception e)
            {}


        }else
        {
            try
            {               
            mSwitcher1.setText(mTextNames1[mTextNames1.length-1]);
            mSwitcher1.startLayoutAnimation();              
            mSwitcher1.getDrawingTime();
            g.setSelection(mTextNames1.length-1);
            Thread.sleep(300);
            g.scheduleLayoutAnimation();
            g.startLayoutAnimation();
            Thread.sleep(300);
            g.setAnimationDuration(motiongal);                  
            }
            catch(Exception e)
            {}

        }
            }
      } 
};    


public void onNothingSelected(AdapterView<?> parent) {
}

public View makeView() {
    TextView i = new TextView(this);
    i.setTextSize(18);

    i.setGravity(1);
    return i;
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getView() {
        // TODO Auto-generated method stub
        return 0;
    }

    public int getCount() {

          countk2 = getIntent().getIntExtra("k1", 0); 
        return  countk2;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
         i = new ImageView(mContext);



        if(countk1==1)
        {
            mSwitcher1.setVisibility(View.INVISIBLE);
            i.setImageResource(mPhotos.get(position));
        }
        if(countk1==2)
        {
            mSwitcher.setVisibility(View.INVISIBLE);
            i.setImageResource(mPhotos1.get(position));
            }

        i.setScaleType(ImageView.ScaleType.FIT_XY);
        if(wt >= 480){
            if(wt == 480 && ht > 320) {
                i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            } else {
                i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }

        }else{              

            i.setLayoutParams(new Gallery.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }

        return i;
    }

    private Context mContext;

}


private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
      return e2.getX() > e1.getX();
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
      int kEvent;
      if(isScrollingLeft(e1, e2)){ //Check if scrolling left
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
      }
      else{ //Otherwise scrolling right
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
      }
      onKeyDown(kEvent, null);
      return true;  
    }


private Integer[] mImageIds = {
         R.drawable.bokstavslottet01,
         R.drawable.bokstavslottet02,
         R.drawable.bokstavslottet03,
         R.drawable.bokstavslottet04,
         R.drawable.bokstavslottet05,
         R.drawable.bokstavslottet06,
         R.drawable.bokstavslottet07,
         R.drawable.bokstavslottet08,
         R.drawable.bokstavslottet09,
         R.drawable.bokstavslottet10,
         R.drawable.bokstavslottet11,
         R.drawable.bokstavslottet12,
         R.drawable.bokstavslottet13
        };
private Integer[] mImageIdsf = {
        R.drawable.f1,
        R.drawable.f2,
        R.drawable.f3,
        R.drawable.f4,
        R.drawable.f5,
        R.drawable.f6,
        R.drawable.f7,
        R.drawable.f8,
        R.drawable.f9,
        R.drawable.f10,
        R.drawable.f11,
        R.drawable.f12,
        R.drawable.f13,
        R.drawable.f14,
        R.drawable.f15,
        R.drawable.f16,

 };
private static String[] mTextNames = {
"BOOKSTOVEN1", "BOOKSTOVEN2", "BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6", "BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10", "BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13"};


private static String[] mTextNames1 = {
    "BOOKSTOVEN1", "BOOKSTOVEN2", "BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6", "BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10", "BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13", "BOOKSTOVEN14", "BOOKSTOVEN15", "BOOKSTOVEN16"};

private ArrayList<Integer> mPhotos = new ArrayList<Integer>(Arrays.asList(mImageIds));
private ArrayList<Integer> mPhotos1 = new ArrayList<Integer>(Arrays.asList(mImageIdsf));
   }
公共类Home扩展活动实现OnClickListener{
按钮b1、b2;
int countk3;
int countk4=0;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.home1);
b1=(按钮)findViewById(R.id.button1);
b2=(按钮)findViewById(R.id.button2);
b1.setOnClickListener(本);
b2.setOnClickListener(本);
b2.设置启用(错误);
countk3=getIntent().getIntExtra(“k3”,0);
如果(countk3==10&&countk4==1)
{
b2.设置启用(真);
b2.挫折资源(R.颜色.黄色);
}
else if(countk3==10&&countk4!=1)
{
b2.设置启用(错误);
b2.挫折资源(红色);
}
}
公共void onClick(视图v){
//TODO自动生成的方法存根
如果(v==b1)
{
countk4++;
Log.i(“值…”,“”+countk4);
意向i=新意向(Home.this,GalleryActivity.class);
i、 putExtra(“k”,1);
i、 putExtra(“k1”,13);
星触觉(i);
}
如果(v==b2)
{
意向i=新意向(Home.this,GalleryActivity.class);
i、 putExtra(“k”,2);
i、 putExtra(“k1”,16);
星触觉(i);
}
}
}
公共类GalleryActivity扩展了活动实现
AdapterView.OnItemSelectedListener、ViewSwitcher.ViewFactory{
专用文本切换器mSwitcher;
整数计数=10;
专用文本切换器mSwitcher1;
g廊;
静态int nextbtn=0;
静态int prebtn=0;
图像适配器;
int galpos;
int countk1=0;
int countk2;
公共int ht;
公共int wt;
字符串s;
int l11;
公共静态int motiongal=3000;
线性布局l1;
整数长度=0;
int长度1;
图像视图i;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.main);
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht=displaymetrics.heightPixels;
wt=displaymetrics.widthPixels;
l1=(线性布局)findViewById(R.id.linear);
mSwitcher=(TextSwitcher)findviewbyd(R.id.switcher);
mSwitcher.setFactory(本);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.fade_in);
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.fade_out);
mSwitcher.startayOutAnimation();
mSwitcher1=(TextSwitcher)findviewbyd(R.id.switcher1);
mSwitcher1.设置工厂(本);
mSwitcher1.setInAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.fade_in);
mSwitcher1.setOutAnimation(AnimationUtils.loadAnimation)(此,
android.R.anim.fade_out);
mSwitcher1.startayOutAnimation();
尝试
{
g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(this));
g、 setHorizontalScrollBarEnabled(假);
Bundle Bundle=getIntent().getExtras();
galpos=bundle.getInt(“galpos”);
g、 选举(galpos);
g、 设置动画持续时间(motiongal);
countk1=getIntent().getIntExtra(“k”,0);
}捕获(例外e){
}
g、 setOnItemSelectedListener(此);
ImageButton nextButton=(ImageButton)findViewById(R.id.nextimage);
setOnClickListener(nextButtonConClick);
ImageButton preButton=(ImageButton)findViewById(R.id.preimage);
preButton.setOnClickListener(PreButtonNonclick);
}
已选择公共位置(AdapterView父视图、视图v、整型位置、长id){
如果(countk1==1)
{
mSwitcher.setText(mTextNames[position]);
}
如果(countk1==2)
{
mSwitcher1.setText(mTextNames1[position]);
}
如果(位置==10)
{
意向i=新意向(GalleryActivity.this,Home.class);
i、 putExtra(“k3”,计数);
startActivityForResult(i,0);
}
nextbtn=位置;
}
public OnClickListener nextButtonOnClick=new OnClickListener(){
公共void onClick(视图v){
如果(countk1==1)
{
mSwitcher1.setVisibility(视图.不可见);
if(nextbtn<(mTextNames.length-1))
{     
尝试
{               
mSwitcher.setText(mTextNames[nextbtn+1]);
mSwitcher.startayOutAnimation();
mSwitcher.getDrawingTime();
g、 设置选择(下一个TBTN+1);
睡眠(300);
g、 ScheduleLayoutImation();
g、 惊人的动画();
睡眠(300);
g、 设置动画持续时间(motiongal);
}
捕获(例外e)
{}
}否则
{
尝试
{               
mSwitcher.setText(mTextNames[0]);
mSwitcher.startayOutAnimation();
mSwitcher.getDrawingTime();
g、 选举(0);
睡眠(300);
g、 ScheduleLayoutImation();
g、 惊人的动画();
睡眠(300);
g、 设置动画持续时间(motiongal);
}
捕获(例外e)
{}
}
}
如果(countk1==2)
{
mSwitcher.setVisibility(View.INVISIBLE);
if(nextbtn<(mTextNames1.长度-1))
{     
尝试
{               
mSwitcher1.setText(mTextNames1[nextbtn+