2013-02-21 77 views
1

我有5个按钮,每个都有字母(按钮“H”,按钮“E”按钮“L”按钮“L”按钮“O”),使单词“HELLO”。我需要的是点击顺序到这些按钮,所以如果我先点击“H”和“E”秒直到完成单词应用程序将做的事情,但如果我先点击“L”会给我一些错误信息。Android onclick按钮序列

任何想法做这个序列?

谢谢

+5

所以,你尝试过什么? – SudoRahul 2013-02-21 05:51:57

+0

嗯,我仍然在考虑如何做到这一点:( – user1376370 2013-02-21 05:54:32

+0

我会给你一个建议,然后试一试,然后回到这里,如果你在任何地方都卡住了。“在每个按钮的'click'上,做2首先,检查前一个按钮被点击**是否被点击,接下来,根据上述“**条件**”将该按钮的标志设置为“点击”或不是“未点击”。 – SudoRahul 2013-02-21 05:55:21

回答

0

Ĵ乌斯季有一个这样的数组:

int[] tracker = new int[5]; 

,当你点击一个按钮,说:“H”,设置tracker[0] = 1;

但是当你点击一个按钮,说:“L”,检查是否所有前面的按钮值是1。如果是,则将相应的跟踪器设置为1 else,显示错误消息,并且不要对跟踪器阵列进行任何更改。

像这样:

onHClick{ 

tracker[0] = 1; 
} 

onEClick{ 

for(int i=0; i<1; i++){ 
if{ 
tracker[i] == 0; 
//show error message and return; 
}else{ 
tracker[1] = 1; 
return; 
} 
} 
} 
1

我不知道你的流量,但你可以试试这个。

  1. 将标签设置为每个按钮的文本,就像这样。

    b.setTag(“H”);

而不是像这样。

Button b; 
String name = ""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    b.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      String s = (String) v.getTag(); 

      name +=s; 

          if("HELLO".startsWith(name)){ 
            <VALID> 
          }else{ 
            <ERROR> 
          } 
     } 
    }); 

} 

支票上的每个按钮的变量name点击你的原词即HELLO像上面。

+0

谢谢,这个方法在你输入完成后会检查的问题,但正如我在问题中提到的那样,如果你先点击L,一个错误信息或者什么也会发生,如果你点击H然后点击O会给你同样的错误信息,我想我只需要知道如何在点击后标记任何按钮你可以帮助我,这将是伟大的。 – user1376370 2013-02-21 06:42:19

+0

是的,明白了,看到我编辑的答案。 – 2013-02-21 07:29:29

0

你可以做这样的事情

当活动开始,你只是做类似的东西其他按钮Enabled = false。但使第一个按钮启用。 不要使Visible=false

现在单击按钮“H”使启用按钮“E”等。

所以用户只需点击按钮的顺序。按钮不能以任何随机方式按下。

试试这个,让我知道它的工作与否。

1

非常有趣和完全相同您requirement..check一次..

如果你给比HELLO也工作得更好其他任何字符串。

public class ButtonTest extends Activity 
{ 
    private String result=""; 
    String sampleText = "HELLO"; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     int noOfBtns  = sampleText.length(); 

     LinearLayout ll = (LinearLayout)findViewById(R.id.btnlay); 
     final TextView tvtext = (TextView)findViewById(R.id.result); 

     final Button[] btns  = new Button[noOfBtns]; 

     for(int i=0;i<noOfBtns;i++) 
     { 
      btns[i] = new Button(this); 
      btns[i].setText(sampleText.substring(i,i+1)); 

      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      ll.addView(btns[i], lp); 

      final int j = i; 
      btns[i].setOnClickListener(new OnClickListener() { 

       public void onClick(View v) 
       { 
        System.out.println(j+" "+result.length()); 
        if(j == result.length()) 
        { 
         result = result+btns[j].getText().toString(); 

         if(sampleText.startsWith(result)) 
         { 
          tvtext.setText(result); 
         } 
        } 
        else 
        { 
         Toast.makeText(getApplicationContext(), "Wrong Button Pressed", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
     } 
    } 
} 

布局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/result" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:textColor="#fff"/> 

    <LinearLayout 
     android:id="@+id/btnlay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal"> 

    </LinearLayout> 

</LinearLayout> 
1

尝试使用以下

package com.example.buttonsequence; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity 
{ 

    ArrayList<Button> buttonList=null; 
    TextView resultTextView=null; 
    Button buttons[]=null; 
    String helloStr="HELLO"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     buttonList=new ArrayList<Button>(); 
     buttons=new Button[5]; 

     this.resultTextView=(TextView) this.findViewById(R.id.result_text); 
     this.resultTextView.setText(""); 

     buttons[0]=(Button)this.findViewById(R.id.h_button); 
     buttons[1]=(Button)this.findViewById(R.id.e_button); 
     buttons[2]=(Button)this.findViewById(R.id.l_button); 
     buttons[3]=(Button)this.findViewById(R.id.l2_button); 
     buttons[4]=(Button)this.findViewById(R.id.o_button); 

     for(int k=0;k<5;k++) 
      buttons[k].setOnClickListener(onClickListener); 

     Button button=(Button)this.findViewById(R.id.exit_button); 
     button.setOnClickListener 
     (
       new OnClickListener() 
       { 

        @Override 
        public void onClick(View arg0) { 
         // TODO Auto-generated method stub 
         finish(); 
        } 

       } 
     ); 
    } 

    OnClickListener onClickListener=new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 

      Button b=(Button)v; 
      buttonList.add(b); 

      int size=buttonList.size(); 
      if(size>0) 
      { 
       StringBuilder resultBuilder=new StringBuilder(); 
       for(int i=0;i<size;i++) 
       { 
        Button tempButton=buttonList.get(i); 
        if(tempButton==buttons[i]) 
        { 
         resultBuilder.append(helloStr.charAt(i)); 

         if(i==4) 
         { 
          resultTextView.setText(resultBuilder.toString()+" clicked"); 
          buttonList.clear(); 
         } 
         else 
         { 
          resultTextView.setText(resultBuilder.toString()+" clicked"); 
         } 

        } 
        else 
        { 
         buttonList.remove(i); 
         Toast.makeText(getApplicationContext(), "No correctly clicked", Toast.LENGTH_SHORT).show(); 
         break; 
        } 
       } 
      } 
      else 
      { 
       resultTextView.setText("Invalid pressed"); 
      } 

     } 

    }; 




} 

activity_main.xml中

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:id="@+id/h_button" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="H" /> 

    <Button 
     android:id="@+id/e_button" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="E" /> 

    <Button 
     android:id="@+id/l_button" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="L" /> 

    <Button 
     android:id="@+id/l2_button" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="L" /> 

    <Button 
     android:id="@+id/o_button" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="O" /> 

    <TextView 
     android:id="@+id/result_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <Button 
     android:id="@+id/exit_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Exit" /> 

</LinearLayout>