有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java事件关注EditText

我有一个EditText,其中EditText获得焦点,它会改变EditText的功能,当我失去EditText的焦点时,它会再次改变功能

这是我的源代码:

tambah = (EditText)rootView.findViewById(R.id.total);
tambah.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View arg0, boolean arg1) {
    try {
        if(..) //i want to check, i get focus
        {...}else //if i loss focus, the textbox do something
        {...} 

    } catch (Exception e) {
    }

}

对于第一次单击edittext时的结果,edittext没有执行任何操作。当我输入“1000”并失去焦点时,edittext变为“1.000”。但当我再次单击edittext时,它又变为“1000”。请帮帮我,如果我的语法不好,请原谅


共 (1) 个答案

  1. # 1 楼答案

    布尔标志=真

     tambah = (EditText)rootView.findViewById(R.id.about_header);
                tambah.setOnFocusChangeListener(new OnFocusChangeListener() {
    
                    public void onFocusChange(View v, boolean hasFocus) {
                        flag = !flag;
                        if(v == tambah && flag) {
                            //format your text to 1000 here
                        }
                        else if(v == tambah && !flag) {
                            //format your text to 1.000 here
                        }
                    }
                });
    

    编辑: 如果你需要一个简单的格式化程序,你可以使用十进制格式化程序

    double d = 1.000;
    DecimalFormat df1 = new DecimalFormat("#.###");
    DecimalFormat df2 = new DecimalFormat("####");
    String newD1 = df1.format(d); // 1.000
    String newD2 = df2.format(d); // 1000