按钮单击不起作用。视图在按钮上发布消息

时间:2013-01-01 19:30:39

标签: android button view

现在我在调试代码时遇到了问题,我的应用程序中的按钮没有响应单击操作。我在点击视图方法上保留了断点,但点击查看方法没有响应。

我在点击方法中所做的是,如果我点击第一个按钮然后用空按钮值交换值。但是点击没有响应。

这是我要检查的完整代码。

public class Play extends Activity implements OnClickListener
{   
private ArrayList<Integer> m_buildButtonsIDs;
private ArrayList<Integer> m_gameButtonsIDs;

public static Character UserWords;
public static Character RemainingWords;
public static Character OpenCard;

static ArrayList<Character> ShuffledCards = new ArrayList<Character>();
static ArrayList<Character> UserBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWordsAfterShowCard = new ArrayList<Character>();
static ArrayList<Character> DroppedCards = new ArrayList<Character>();

static Stack<Character>UserDroppedCards = new Stack<Character>();

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);

    InitializeBoard();  
}

public void InitializeBoard() 
{      
  m_buildButtonsIDs = new ArrayList<Integer>();

  m_buildButtonsIDs.add(R.id.Button1);
  m_buildButtonsIDs.add(R.id.Button2);
  m_buildButtonsIDs.add(R.id.Button3);
  m_buildButtonsIDs.add(R.id.Button4);
  m_buildButtonsIDs.add(R.id.Button5);
  m_buildButtonsIDs.add(R.id.Button6);
  m_buildButtonsIDs.add(R.id.Button7);
  m_buildButtonsIDs.add(R.id.Button8);
  m_buildButtonsIDs.add(R.id.Button9);

  m_gameButtonsIDs = new ArrayList<Integer>();

  m_gameButtonsIDs.add(R.id.pickButton);
  m_gameButtonsIDs.add(R.id.showButton);
  m_gameButtonsIDs.add(R.id.emptyButton);

  String random = RandomAlphabetGenerator.Random();

  for(int i = 0;i<random.length();i++)
  {
    char randomcards = random.charAt(i);            

    ShuffledCards.add(randomcards);
  }

  for(int i = 0;i < 9;i++)
  { 
    UserWords = ShuffledCards.get(i);
    UserBuildWords.add(UserWords);

        if(i == 8)
        {
            for(int j = 9;j < 52;j++)
            {   
                RemainingWords = ShuffledCards.get(j);
                RemainingBuildWords.add(RemainingWords);
            }
        }
    }   

    OpenCard = RemainingBuildWords.get(0);

    DroppedCards.add(OpenCard);

    RemainingBuildWords.remove(0);
    RemainingBuildWordsAfterShowCard.addAll(RemainingBuildWords);

    FillUserBuildButtons(ShuffledCards);
    StackingDroppedButtons(DroppedCards);
    StackingPickButtons(RemainingBuildWordsAfterShowCard);
}   

private void FillUserBuildButtons(ArrayList<Character> shuffledCards)
{       
    for (int i=0 ; i<m_buildButtonsIDs.size() ; i++) 
    {
        Button BuildButton = (Button)findViewById(m_buildButtonsIDs.get(i));
        BuildButton.setText(UserBuildWords.get(i).toString());          
    }
}

private void StackingDroppedButtons(ArrayList<Character> droppedCards)
{       
    Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(1));
    ShowButton.setText(DroppedCards.get(0).toString());     
}

private void StackingPickButtons(ArrayList<Character> remainingBuildWordsAfterShowCard)
{       
    Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(0));
    ShowButton.setText(RemainingBuildWordsAfterShowCard.get(0).toString());     
}

public void onClick(View v)
{       
    CharSequence text;

    switch(v.getId())
    {
      case R.id.Button1:

          Button FirstButton = (Button)findViewById(m_buildButtonsIDs.get(0));
          text = FirstButton.getText().toString();
          FirstButton.setText(SwapValue(text)); 
          break;

      case R.id.emptyButton:              
          break;
    }   
}

private CharSequence SwapValue(CharSequence k) 
{
    CharSequence empty = null;

    Button EmptyButton = (Button)findViewById(m_buildButtonsIDs.get(0));
    empty = EmptyButton.getText().toString();
    EmptyButton.setText(k);

    return empty;
}           

}

这是我检查的xml代码:

<TableLayout
android:id="@+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="50dp"
android:layout_marginLeft="115dp">

<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" >

<Button
android:id="@+id/Button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Dummy"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />

<Button
android:id="@+id/Button4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button6"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />
</LinearLayout>

<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" 
android:layout_marginBottom="30dp">

<Button
android:id="@+id/Dummy2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />

<Button
android:id="@+id/Dummy3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />

<Button
android:id="@+id/Button7"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button8"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp" />

<Button
android:id="@+id/Button9"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp"/>

<Button
android:id="@+id/Dummy4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />

<Button
android:id="@+id/Dummy5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />

</LinearLayout>

<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" 
android:layout_marginLeft="20dp">

<Button
android:id="@+id/pickButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp"/>

<Button
android:id="@+id/showButton" 
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/button_label"
android:textSize="15dp"/>

<Button
android:id="@+id/emptyButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/empty"
android:textSize="15dp" />

<Button
android:id="@+id/emptyButton1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text=""
android:textSize="15dp" />

<Button
android:id="@+id/dropButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/drop"
android:textSize="15dp"/>

<Button
android:id="@+id/declareButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="@string/declare"
android:textSize="15dp"/>

</LinearLayout>
</TableLayout>

1 个答案:

答案 0 :(得分:0)

您应该向onClickListener添加Button

btn.setOnClickListener(Play.this);