-1

私は編集テキストを持っていました.4桁の数字を入力するために使用した編集テキストで、いくつかのキーと値のペアを0-9の数字に設定しました.

したがって、私の質問は、編集テキストに4桁の数字(任意の数字)を入力するたびに、メッセージが表示されるように1桁の数字に変換する必要があるというkeyvalueことです。

これは私のコードです:

public class MainActivity extends Activity {

     String Sequence;
     Button buttonok;
     String UserEntreredNumber;
     HashMap<String,String> messagesMap = new HashMap<String,String>();
     String magicMessage;

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

        final EditText Sequence = (EditText)this.findViewById(R.id.Sequence);    

        Sequence.setError("Input must be 4 digits");



        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton(" ok ",  new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                dialog.cancel();
          }
        });

        //create hash map

       populateList(); 

        //populate hash map with 0 to 9 key and values


        buttonok=(Button)this.findViewById(R.id.buttonok);
        buttonok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                UserEntreredNumber = Sequence.getText().toString();
                magicMessage = messagesMap.get(UserEntreredNumber);

                builder.setMessage( magicMessage);
                builder.show();         
        }
    });     

}
    private void populateList() { 

       messagesMap.put("0", "Congratulations!!!, you have been selected."); 
       messagesMap.put("1", "Wow! your program just ran without errors!"); 
       messagesMap.put("2", "It's very hot in office"); 
       messagesMap.put("3", "Wow, what a building? It's awesome");
       messagesMap.put("4", "You got a calll");
       messagesMap.put("5", "There were no errors");
       messagesMap.put("6", "U have been shortlisted for the next round");
       messagesMap.put("7", "nice costume");
       messagesMap.put("8", "do u have any idea!");
       messagesMap.put("9", "Today is a bad day");    
    }   
}
4

2 に答える 2

1

多分これはあなたを助けるでしょう:

Integer digit; 
try{
   digit = Integer.valueOf(Sequence.getText().toString());
} catch (NumberFormatException e){
   digit = null;
}
if (digit != null && digit >= 0 && digit <= 9999 && Pattern.matches("^[0-9]{4,4}$", Sequence.getText().toString());){
     //some logic
} else {
   Sequence.setError("Input must be 4 digits");
}  
于 2012-12-28T09:12:26.863 に答える
0

あなたが言った:

4 桁の数字を 1 桁の数字に変換する必要があります

それが必要な場合は、4 桁の数字を 10 で割った余り、つまり演算の結果を取得します。

your 4 digit number % 10

0 から 9 までの数値を返します。実際には 4 桁の数値の最後の桁です。


変数 digit4 に 4 桁の数字があるとします。

Integer digit4 = Integer.valueOf(Sequence.getText().toString());

次に、変数 digit1 で 0 から 9 までの 1 桁の数字 (実際には 4 桁の数字の最後の数字) を取得できます。

                int digit1 = digit4 % 10;

4 桁と 1 桁の数値の関係を指定していないため、入力に関係なく 0 から 9 までの乱数が必要な場合は、次のようなpublic int nextInt (int n)メソッドを使用して取得できます。

Random r= new Random();
int digit1 = r.nextInt(10);
于 2012-12-28T09:53:34.317 に答える