2016-07-24 49 views
-1

我有一些Java代码,我想在每次按下活动中的任何按钮时触发它。我将每个按钮设置为具有android的属性:onClick =“onClick”。但是,我收到了几个错误代码,不确定如何处理它们。我得到了“变量onClick从不使用”,“无法解析符号视图”和“无法为最终变量q赋值”。我能对每一个做什么?非常感谢!!如何让多个按钮触发onClick()并解析符号视图

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.content.res.Resources; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Button; 
import java.lang.String; 
import java.util.Random; 


public class MainActivity extends AppCompatActivity { 

    private static final Random r_generator = new Random(); 
    String textViewString; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Resources res = getResources(); 
     final TextView tv = (TextView) findViewById(R.id.color_text); 
     final String[] myString = res.getStringArray(R.array.englishColorArray); 
     final String q = myString[r_generator.nextInt(myString.length)]; 
     tv.setText(q); 
     textViewString = tv.getText().toString(); 

     @Override 
     public void onClick(View view) { 
      //This casts your view to be a button so you can access its features as a button 
      textViewString = tv.getText().toString(); 
      Button btn = (Button) view; 

      if(!btn.getText().equals(textViewString)){ 
       q = myString[r_generator.nextInt(myString.length)]; 
       tv.setText(q); 
       //Perform action to notify user that they pressed the wrong button 
       //Do not return here. This is what caused the program to lock up 
       //Maybe add a TextView that says correct/incorrect and can change that when the user is correct or incorrect 
      } 

      //Gets a random color from my string array and sets it to the TextView 
      q = myString[r_generator.nextInt(myString.length)]; 
      tv.setText(q); 
     } 

和XML代码...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@android:style/Theme.Holo.Light" 
    tools:context="com.example.cedric.learnthecolors.MainActivity" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/color_text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textSize="68sp" 
      android:gravity="center" 
      android:textAllCaps="true"/> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/green_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Green" 
      android:textSize="0sp" 
      android:background="@drawable/green_button"/> 

     <Button 
      android:id="@+id/blue_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Blue" 
      android:textSize="0sp" 
      android:background="@drawable/blue_button"/> 

     <Button 
      android:id="@+id/red_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Red" 
      android:textSize="0sp" 
      android:background="@drawable/the_red_button"/> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/yellow_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Yellow" 
      android:textSize="0sp" 
      android:background="@drawable/yellow_button"/> 

     <Button 
      android:id="@+id/white_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/White" 
      android:textSize="0sp" 
      android:background="@drawable/white_button"/> 

     <Button 
      android:id="@+id/orange_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Orange" 
      android:textSize="0sp" 
      android:background="@drawable/orange_button"/> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/linearLayout3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/brown_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Brown" 
      android:textSize="0sp" 
      android:background="@drawable/brown_button"/> 

     <Button 
      android:id="@+id/pink_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Pink" 
      android:textSize="0sp" 
      android:background="@drawable/pink_button"/> 

     <Button 
      android:id="@+id/purple_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:onClick="onClick" 
      android:text="@string/Purple" 
      android:textSize="0sp" 
      android:background="@drawable/purple_button"/> 

    </LinearLayout> 

</LinearLayout> 

回答

0

这是因为集成了您的onCreate内的onClick(或者你根本都忘了,使右括号“}”),所以按键可以”达到该方法。插入缺少的支架,像这样:

public class MainActivity extends AppCompatActivity { 

    private static final Random r_generator = new Random(); 
    String textViewString; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Resources res = getResources(); 
     final TextView tv = (TextView) findViewById(R.id.color_text); 
     final String[] myString = res.getStringArray(R.array.englishColorArray); 
     final String q = myString[r_generator.nextInt(myString.length)]; 
     tv.setText(q); 
     textViewString = tv.getText().toString(); 
    } // this bracket here was missing 

    @Override 
    public void onClick(View view) { 
     //This casts your view to be a button so you can access its  features as a button 
     textViewString = tv.getText().toString(); 
     Button btn = (Button) view; 

     if(!btn.getText().equals(textViewString)){ 
      q = myString[r_generator.nextInt(myString.length)]; 
      tv.setText(q); 
      //Perform action to notify user that they pressed the wrong button 
      //Do not return here. This is what caused the program to lock up 
      //Maybe add a TextView that says correct/incorrect and can change that when the user is correct or incorrect 
     } 

     //Gets a random color from my string array and sets it to the TextView 
     q = myString[r_generator.nextInt(myString.length)]; 
     tv.setText(q); 
    } 
0
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 
//some code 
    @override 
    onCreate(bundle SavedState){ 
    //some code 
     button1.setOnClickListener(this); 
     button2.setOnclickListener(this); 
    } 

    //your click listener 
    @override 
    onClick(View view){ 
     int id = view.getID(); 
    } 
} 

总之,使用点击你的代码是搞砸监听器接口

+0

用户希望将其设置为XML,而不用费心寻找并设置代码中的每一个。 – lionscribe

+0

但实现相同的onClickListener方法最好通过接口完成。在XML中,会使可读性复杂化。 –

+0

看不出为什么。他们所做的只是在XML中添加onClick属性,并在Activity中提供一个OnClick函数。使用Fragments时确实很麻烦。 – lionscribe

0


  1. 您的活动应声明为实现View.OnclickListener接口。
  2. 您的onClick 函数应该在Activity类中,而不是在onCreate
    函数中。
  3. 您应该将q声明为您类的成员变量,并且不应该标记为final。