Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在onClick中做出决策_Android_Onclick - Fatal编程技术网

Android 如何在onClick中做出决策

Android 如何在onClick中做出决策,android,onclick,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) findViewByI

这用于搜索
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));
    }
我怎么能这样做?如有任何建议,将不胜感激:)


注:我是android编程新手。如果您也能帮助我编写代码,我将不胜感激。以下是如何实现单击计数器

请注意,当总共记录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;
        }
    }
});
//在之前查找文本视图(这样您就不必重复这个昂贵的操作)
最终文本视图tv1=findViewById(R.id.textView1);
最终文本视图tv2=findViewById(R.id.textView2);
imageButton.setOnClickListener(新视图.OnClickListener(){
int numberOfClicks=0;
@凌驾
公共void onClick(视图){
++点击次数;
如果(点击次数<2){
tv1.setText(“某些文本”);
}否则如果(点击次数>=2){
tv1.setText(“其他文本”);
//重置单击计数器
点击次数=0;
}
}
});

您可以使用类变量将其值更改为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建议)

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

     //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;
                   }
    ...
    ...
    ...
        }

创建一个
int
变量作为计数器,单击按钮时将其递增,并检查
if/else
中的数字。您能帮我编码吗?这是我的第一个应用程序,所以我不太擅长构建代码。你是说连续两次点击
ImageButton
?是的,但是如果我想要超过两次,会有区别吗?因为my taget如果至少连续单击10次(如果可能的话),那么单击是在文本视图上放置单词后进行的。请显示
OnClickListener的所有代码。现在还不清楚你到底想做什么。为什么不使用布尔值?我已经编辑了我的问题,包括了我正在使用的代码。。你能用这些代码帮我解决问题吗?很抱歉,排版错误应该是ImageView。我在xml中将其声明为android:onClick=“viewWord”。我希望你能理解我。@Elenasys它不起作用了。请在第一次单击时查看上面的图像。它应位于第二个文本视图,第二次单击应位于第三个文本视图。我需要这样做,因为我会把它们都加起来,并在第一个文本视图中显示总和。我有上面的代码。你能帮我做这个吗?
    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;
                   }
    ...
    ...
    ...
        }