如何在onClick上做出决定

时间:2014-10-24 18:25:38

标签: android onclick

这是用于搜索TextView

上的字词
 public void viewWord(View view)
    {

        score = (TextView)findViewById(R.id.yourScore);
        tv2 = (TextView)findViewById(R.id.tv2);
        tv3 = (TextView)findViewById(R.id.tv3);
        searchWord = (ImageView) findViewById(R.id.searchWord);
        wordList = (ListView) findViewById(R.id.wordList);
        text = (AutoCompleteTextView) findViewById(R.id.textHere);
        wordList = (ListView) findViewById(R.id.wordList);

      //SearchWord in AutoCompleteTextView text
      //Display equivalent score of word in TextView score
        String s1= search.getText().toString();
        String s2= dbHelper.getData(s1);
        score.setText(s2);
        tv2.setText(s2);
        tv3.setText(s2);

      //Get data from textview      
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
        wordList.setAdapter(adapter);       
        adapter.add(text.getText().toString());
        adapter.notifyDataSetChanged();
        text.setText("");
        add();


    }

    public void add (){
        x = Integer.parseInt(tv2.getText().toString());
        y = Integer.parseInt(tv3.getText().toString());
        z = x + y;
        score.setText(Double.toString(z));
    }

在我的应用中,我使用ImageView搜索字词并显示分数。我很难用1 TextView显示得分,所以我决定成功2.我希望我的ImageView能够像这样运作:

if(onMyImageViewFirstClick){
display the score on textview1
}
else{
if(onMyImageViewSecondClick){
display the score on textview2
}

我怎么能这样做?任何建议将受到高度赞赏:)

注意:我是android编程的新手。如果您能帮助我使用代码,我将不胜感激

2 个答案:

答案 0 :(得分:2)

您可以使用类变量changin将其值设置为0 - 1,例如:

private int optionTxtView = 0

    imageButton = (ImageButton) findViewById(R.id.imageButton1); 
            imageButton.setOnClickListener(new OnClickListener() { 
                @Override
                public void onClick(View arg0) {
                   if(optionTxtView == 0){
                     //display the score on textview1
                    optionTxtView = 1;
                   }else{
                     //display the score on textview2    
                   optionTxtView = 0;
                   }
                }
            });

或者您可以使用布尔值(由@mapo建议)

    private boolean optionTxtView;

        imageButton = (ImageButton) findViewById(R.id.imageButton1); 
                imageButton.setOnClickListener(new OnClickListener() { 
                    @Override
                    public void onClick(View arg0) {
                       if(optionTxtView){
                         //display the score on textview1
                        optionTxtView = false;
                       }else{
                         //display the score on textview2    
                       optionTxtView = true;
                       }
                    }
                });

因此,根据您的代码,解决方案必须是:

     //declare a boolean variable
private int optionTxtView = 0  

     public void viewWord(View view)  {
    ...
    ...
    ...
          //SearchWord in AutoCompleteTextView text
          //Display equivalent score of word in TextView score
            String s1= search.getText().toString();
            String s2= dbHelper.getData(s1);
            score.setText(s2);

                   if(optionTxtView == 0){
                         //display the score on textview1
                        tv2.setText(s2);
                        optionTxtView = 1;
                   }else{
                         //display the score on textview2    
                         tv3.setText(s2);
                       optionTxtView = 0;
                   }
    ...
    ...
    ...
        }

答案 1 :(得分:0)

以下是如何实施点击计数器。

注意在注册总共2次点击时如何重置计数器。一旦发生这种情况,计数器将重新开始,首先会影响tv1,然后tv2

// Find TextViews before (so you don't have to repeat this expensive operation)
final TextView tv1 = findViewById(R.id.textView1);
final TextView tv2 = findViewById(R.id.textView2);
imageButton.setOnClickListener(new View.OnClickListener() {
    int numberOfClicks = 0;

    @Override
    public void onClick(View view) {
        ++numberOfClicks;
        if(numberOfClicks < 2){
            tv1.setText("Some text");
        } else if(numberOfClicks >= 2){
            tv1.setText("Some other text");
            // Reset the click counter
            numberOfClicks = 0;
        }
    }
});