我有一个Alert Dialog构建器,它显示EditText Box和其他几个。现在我需要 将其转换为普通视图(父视图)。
添加代码:
public synchronized void showPopUp(final String picturePath){
ScrollView scrollView = new ScrollView(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout alertLayout = new LinearLayout(this);
InputFilter[] inputFilter = new InputFilter[1];
inputFilter[0] = new InputFilter.LengthFilter(50);
alertLayout.setOrientation(1); // 1 is for vertical orientation
final EditText nameInput = new EditText(this);
nameInput.setHint("Receipt Name");
nameInput.setFilters(inputFilter);
;
nameInput.setSingleLine(true);
final TextView dateText = new TextView(this);
dateText.setText("Receipt Date");
final EditText amtInput = new EditText(this);
amtInput.setHint("Receipt Amount");
amtInput.setSingleLine(true);
DigitsKeyListener digitKeyListener = new DigitsKeyListener(false, true);
amtInput.setKeyListener(digitKeyListener);
InputFilter[] amtInputFilter = new InputFilter[2];
amtInputFilter[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart)
+ source.subSequence(start, end)
+ destTxt.substring(dend);
if (!resultingTxt
.matches("^\\$?\\-?([1-9]{1}[0-9]{0,2}(\\,\\d{3})*(\\.\\d{0,2})?|[1-9]{1}\\d{0,}(\\.\\d{0,2})?|0(\\.\\d{0,2})?|(\\.\\d{1,2}))$|^\\-?\\$?([1-9]{1}\\d{0,2}(\\,\\d{3})*(\\.\\d{0,2})?|[1-9]{1}\\d{0,}(\\.\\d{0,2})?|0(\\.\\d{0,2})?|(\\.\\d{1,2}))$|^\\(\\$?([1-9]{1}\\d{0,2}(\\,\\d{3})*(\\.\\d{0,2})?|[1-9]{1}\\d{0,}(\\.\\d{0,2})?|0(\\.\\d{0,2})?|(\\.\\d{1,2}))\\)$")) {
return "";
}
}
return null;
}
};
amtInputFilter[1] = new InputFilter.LengthFilter(13);
amtInput.setFilters(amtInputFilter);
amtInput.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN)
return false;
if (actionId == EditorInfo.IME_ACTION_NEXT
|| event == null
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(amtInput.getWindowToken(), 0);
}
return false;
}
});
final DatePicker datePicker = new DatePicker(this);
datePicker.clearFocus();
datePicker.bringToFront();
alertLayout.addView(nameInput);
alertLayout.addView(amtInput);
alertLayout.addView(dateText);
alertLayout.addView(datePicker);
scrollView.addView(alertLayout);
alert.setView(scrollView);
alert.setTitle("Enter Expense Detail");
alert.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
rcptName = nameInput.getText().toString();
rcptAmt = amtInput.getText().toString();
int spinnerDate = datePicker.getDayOfMonth();
int spinnerMonth = datePicker.getMonth() + 1;
if (spinnerMonth < 10 && spinnerDate < 10) {
rcptDate = (datePicker.getYear()) + "0" + (spinnerMonth)
+ "0" + spinnerDate;
} else if (spinnerMonth < 10) {
rcptDate = (datePicker.getYear()) + "0" + (spinnerMonth)
+ "" + spinnerDate;
} else if (spinnerDate < 10) {
rcptDate = (datePicker.getYear()) + "" + (spinnerMonth)
+ "0" + spinnerDate;
} else {
rcptDate = (datePicker.getYear()) + "" + (spinnerMonth)
+ "" + spinnerDate;
}
SavePreferences("RNAME", rcptName);
SavePreferences("RAMOUNT", rcptAmt);
SavePreferences("RDATE", rcptDate);
SharedPreferences sharedPreferences = getSharedPreferences(
"LoginPref", 0);
String username = sharedPreferences.getString("USERNAME", null);
String guid = sharedPreferences.getString("GUID", null);
String password = sharedPreferences.getString("PASSWORD", null);
String rcptName = sharedPreferences.getString("RNAME", null);
String rcptAmount = sharedPreferences
.getString("RAMOUNT", null);
String rcptDate = sharedPreferences.getString("RDATE", null);
String uploadLocation = sharedPreferences.getString("LOGINURL",
null);
if (rcptAmount.isEmpty() || rcptName == null
|| rcptName.isEmpty() || rcptName.trim().isEmpty()) {
showPopUp(picturePath);
} else {
if (isInternetOn()) {
new UploadTask().execute(picturePath, username, guid,
rcptName, rcptAmount, rcptDate, uploadLocation,
password);
} else {
finish();
}
}
}
});
alert.setOnKeyListener(new DialogInterface.OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// whitelist allowed keys - allow navigation keys only
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
});
alert.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
startActivity(getIntent());
}
});
alert.setCancelable(false);
alert.show();
}
请建议。
谢谢, 阿南德