我有一個編輯文本,在我用來輸入一個4位數字的編輯文本,我已經設置了一些鍵和值對0-9號碼。我怎樣才能轉換4位數字在Android中的單個數字號碼
所以我的問題是,當我在編輯文本中輸入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");
}
}
能否請你解釋清楚?如果在編輯文本中輸入4位數字(任意數字),則應該將其轉換爲單個數字編號** –
對於4位數字 – Abhinai
您是否希望輸入總是在1000和9999之間?這些都是四位數字。如果是這種情況,你可以檢查字符串確實是4個字符長,嘗試將其轉換爲整數,如果它轉換,請檢查大小。 – Ewald