1

私は5つのボタンを持っており、それぞれに文字(ボタン「H」、ボタン「E」ボタン「L」ボタン「L」ボタン「O」)があり、「HELLO」という言葉を作っています。私が必要とするのは、これらのボタンのクリックシーケンスを作成することです。そのため、単語が完了するまで最初に「H」をクリックし、次に「E」をクリックすると、アプリは何かを実行しますが、最初に「L」をクリックするとエラーメッセージが表示されます。

このシーケンスを実行するためのアイデアはありますか?

ありがとう

4

5 に答える 5

1

非常に興味深く、要件とまったく同じです..一度確認してください..

以外の文字列を指定すると、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>
于 2013-02-21T06:45:06.087 に答える
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>
于 2013-02-21T07:00:00.820 に答える
1

次のような配列を用意してください。

int[] tracker = new int[5];

ボタンをクリックするときは、「H」と言って、設定しますtracker[0] = 1;

ただし、ボタンをクリックすると、「L」と言って、前のボタンの値がすべて1. はいの場合は、対応するトラッカーを1else に設定し、エラー メッセージを表示して、トラッカー配列を変更しません。

このようなもの:

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;
}
}
}
于 2013-02-21T07:10:11.520 に答える
1

あなたの流れは正確にはわかりませんが、これを試すことができます。

  1. このように、Tag を各ボタンに Text として設定します。

    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

于 2013-02-21T06:27:05.077 に答える
1

次のようなことができます

活動が始まったら、Enabled = falseそのようなものの他のボタンを作るだけです。ただし、最初のボタンを有効にします。を作らないでくださいVisible=false

ボタン「H」をクリックすると、ボタン「E」が有効になります。

したがって、ユーザーはボタンを順番にクリックするだけで済みます。ボタンをランダムに押すことはできません。

これを試して、うまくいくかどうか教えてください。

于 2013-02-21T06:27:33.433 に答える