我有一个编辑文本,在我用来输入 4 位数字的编辑文本中,我将一些键和值对设置为 0-9 数字。
所以我的问题是,当我在编辑文本中输入一个 4 数字(任何数字)时,它应该被转换为一个数字,以便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");
}
}