将变量从OnClick事件传递到其他OnClick事件

时间:2013-05-05 22:20:01

标签: android variables onclick

我在第一个onClick事件中创建了一个变量Tweet。 我需要为另一个onClick事件获取该变量。变量Tweet或变量RandomIndex。我现在正在网上搜索两个小时,有问题要找我如何传递该变量。据我所知,当OnClick事件结束时,它会杀死变量。

buttonWhatToDoNow.setOnClickListener(new View.OnClickListener() {

            public int GetRandomNumber(int max) {
                Random rand = new Random();
                int num = rand.nextInt(max);
                return num;
            }

            int RandomIndex;

            public void onClick(View v) {

                int countMax = databaseHelper.getCountNow();
                RandomIndex = GetRandomNumber(countMax) + 1;

                String Boolean = databaseHelper.getBoolean(RandomIndex);

                if (Boolean == "1") {
                    RandomIndex = GetRandomNumber(countMax) + 1;
                    Boolean = databaseHelper.getBoolean(RandomIndex);
                }
                while (Boolean == "1") {
                    RandomIndex = GetRandomNumber(countMax) + 1;
                    Boolean = databaseHelper.getBoolean(RandomIndex);
                }

                String Title = databaseHelper.getTitleNow(RandomIndex);
                String Text1 = databaseHelper.getText1Now(RandomIndex);
                String Text2 = databaseHelper.getText2Now(RandomIndex);
                String Text3 = databaseHelper.getText3Now(RandomIndex);
                String Tweet = databaseHelper.getTweetNow(RandomIndex);

                String Image1 = databaseHelper.getImage1Now(RandomIndex);
                String Image2 = databaseHelper.getImage2Now(RandomIndex);
                String Image3 = databaseHelper.getImage3Now(RandomIndex);

                textViewDoThisTitle.setText(Title);
                textViewDoThisText1.setText(Text1);
                imageViewDoThis1.setImageResource(getResources().getIdentifier(
                        Image1, "drawable", getPackageName()));
                textViewDoThisText2.setText(Text2);
                imageViewDoThis2.setImageResource(getResources().getIdentifier(
                        Image2, "drawable", getPackageName()));
                textViewDoThisText3.setText(Text3);
                imageViewDoThis3.setImageResource(getResources().getIdentifier(
                        Image3, "drawable", getPackageName()));

            }

            public final int asddd = RandomIndex;
        });

        buttonTweetThis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, Tweet);
                sendIntent.setType("application/twitter");
                startActivity(Intent.createChooser(sendIntent, null));
            }
        });

2 个答案:

答案 0 :(得分:0)

我不知道你为什么不使用类变量,但无论如何你可以试试这个:

 setTag(Object tag)
    //Sets the tag associated with a view.


getTag(Object tag)
//gets the tag associated with a view.


thus place anything you want to save for example view.setTag(tweet);

then in the other onclick yo can get the raw object and probably have to cast it to what you want. using view.getTag(); 

答案 1 :(得分:0)

你可以像在全班变量中一样使用Tweet。如果您有复杂的活动,这不是一个好的解决方案。

您也可以在不同的内部类中放置单击侦听器,并且您可以抛出此类的构造函数在没有全局变量的方法中传递此变量。