Android 使用微调器编辑文本字段

Android 使用微调器编辑文本字段,android,android-edittext,Android,Android Edittext,我希望使用微调器编辑编辑文本框。如果某人选择第一个选项,我想在数字的开头加一个负号。如果一个人选择第二个选项,负号应该消失。这就是我试图做的,我得到了负号,但当我选择另一个选项时它不会消失: @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub switc

我希望使用微调器编辑编辑文本框。如果某人选择第一个选项,我想在数字的开头加一个负号。如果一个人选择第二个选项,负号应该消失。这就是我试图做的,我得到了负号,但当我选择另一个选项时它不会消失:

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.vspin:
        if (arg2 == 0) {
            latitudeinput.setText("" + latitudeinput.getText().toString(),
                    TextView.BufferType.EDITABLE);
        } else {
            latitudeinput.setText("-" + latitudeinput.getText().toString(),
                    TextView.BufferType.EDITABLE);
        }

        break;
    case R.id.hspin:
        if (arg2 == 0) {
            longitudeinput.setText(
                    "" + longitudeinput.getText().toString(),
                    TextView.BufferType.EDITABLE);
        } else {
            longitudeinput.setText("-"
                    + longitudeinput.getText().toString(),
                    TextView.BufferType.EDITABLE);
        }
        break;
    }

}
@覆盖
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
开关(arg0.getId()){
案例R.id.vspin:
如果(arg2==0){
latitudeinput.setText(“+latitudeinput.getText().toString()”,
TextView.BufferType.EDITABLE);
}否则{
latitudeinput.setText(“-”+latitudeinput.getText().toString(),
TextView.BufferType.EDITABLE);
}
打破
案例R.id.hspin:
如果(arg2==0){
longitudeinput.setText(
“”+longitudeinput.getText().toString(),
TextView.BufferType.EDITABLE);
}否则{
longitudeinput.setText(“-”
+longitudeinput.getText().toString(),
TextView.BufferType.EDITABLE);
}
打破
}
}

您需要检查edittext中的第一个字符,如果是“-”,则将其删除

if (arg2 == 0) {
    if(latitudeinput.getText().toString().length()<1){
         latitudeinput.setText("" + latitudeinput.getText().toString();
         return;
    }
    if(latitudeinput.getText().toString().charAt(1)=='-'){
        latitudeinput.setText("" + latitudeinput.getText().toString().substring(1),
        TextView.BufferType.EDITABLE);
    }
}
else{
    latitudeinput.setText("-" + latitudeinput.getText().toString(),
    TextView.BufferType.EDITABLE);
}
if(arg2==0){

如果(latitudeinput.getText().toString().length()在您的案例中是:R.ID.?请使用以下代码

            String s = latitudeinput.getText().toString().trim();
            if (arg2 == 0) {
                if(s.contains("-")){
                    latitudeinput.setText(s.subSequence(1, s.length()),
                        TextView.BufferType.EDITABLE);
                }else{
                    latitudeinput.setText(s.subSequence(1, s,
                        TextView.BufferType.EDITABLE);
                }
            } else {
                if(!s.contains("-")){
                    latitudeinput.setText("-" +s,
                        TextView.BufferType.EDITABLE);
                }else{
                    latitudeinput.setText(s.subSequence(1, s,
                        TextView.BufferType.EDITABLE);
                }
            }

我得到了一个Stringoutofboundexception length=0 index=1如果我使用另一种方法,并通过实现以下代码尝试将带有负号的edittext字符串传递给下一个活动:data1=longitudeinput.getText().toString();data2=latitudeinput.getText().toString();if(spinner.getSelectedItem().equals(“North”){dat1=Double.parseDouble(data1)*-1;}//然后dat1被传递到下一个活动,但这不起作用,因为它给出了负号,但我回到了我的问题所在的位置。在我再次编辑的喷丝头中选择第二个选项时,负号不会消失。你应该试着理解逻辑,而不是依赖我的代码。我正在尝试检查edittext是否符合tains'-'如果是,则尝试删除它。子序列只能接受(int,int),在else语句中给出一个错误