我有一个基本上包含所有表单组件的活动。以前它只是在 EditText 框上应用了一些基本的文本过滤,在我添加了一个功能来单击并使用内置相机的应用程序保存图片之前,它工作正常。
现在,“拍照”部分工作正常,但前两个 Edittext 框中提供的输入没有被过滤。
我在这里错过了什么吗?
另外,一个活动的 onCreate 方法是否只能有一个 setonclick 监听器?
谢谢..
这是我的活动代码:
public class Ques1 extends Activity {
private EditText outletEdit;
private EditText ownerEdit;
private EditText phone1Edit;
private EditText phone2Edit;
private EditText phone3Edit;
private EditText phone4Edit;
private EditText cityEdit;
private EditText stateEdit;
private EditText MrktAreaEdit;
private RadioButton ownVehYes;
private RadioButton ownVehNo;
private static int TAKE_PICTURE = 1;
private Uri outputFileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ques1);
Button outletPic = (Button) findViewById(R.id.Button01_outletpic);
outletPic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
TakePhoto();
}
});
//CUSTOM INPUTFILTER HERE 2.0
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source instanceof SpannableStringBuilder) {
SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source;
for (int i = end - 1; i >= start; i--) {
char currentChar = source.charAt(i);
if (!Character.isLetter(currentChar) && !Character.isSpaceChar(currentChar)) {
sourceAsSpannableBuilder.delete(i, i+1);
}
}
return source;
} else {
StringBuilder filteredStringBuilder = new StringBuilder();
for (int i = 0; i < end; i++) {
char currentChar = source.charAt(i);
if (Character.isLetter(currentChar) || Character.isSpaceChar(currentChar)) {
filteredStringBuilder.append(currentChar);
}
}
return filteredStringBuilder.toString();
}
}
};
this.outletEdit = (EditText) findViewById(R.id.editText1_outlet);
this.outletEdit.setFilters(new InputFilter[]{filter});
this.ownerEdit = (EditText) findViewById(R.id.editText1_owner);
this.ownerEdit.setFilters(new InputFilter[]{filter});
//this.cityEdit = (EditText) findViewById(R.id.editText1_owner);
//this.cityEdit.setFilters(new InputFilter[]{filter});
// LENGTH INPUTFILTERS HERE
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(25);
this.outletEdit.setFilters(FilterArray);
this.ownerEdit.setFilters(FilterArray);
//ownVehYes.setOnClickListener = (RadioButton) findViewById(R.id.radio0);
this.ownVehYes = (RadioButton) findViewById(R.id.radio0);
this.ownVehNo = (RadioButton) findViewById(R.id.radio1);
OnClickListener own_veh_yes_listener = new OnClickListener (){
public void onClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(Ques1.this);
alert.setTitle("How many?");
final EditText input = new EditText(Ques1.this);
//input.setFilters(FilterArray2); //need filter for "how many?..veh field"
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String howmany_veh_value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), howmany_veh_value, Toast.LENGTH_SHORT).show(); //test toast is here
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ownVehNo.setChecked(true);
dialog.cancel();
}
});
alert.show();
}
};
ownVehYes.setOnClickListener(own_veh_yes_listener);
}
private void TakePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK){
//ShowMessage(outputFileUri.toString());
if(data!=null){
Toast.makeText(getApplicationContext(), outputFileUri.toString(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_ques1, menu);
return true;
}
}