Android / Java:将edittext值更改为按下的值以外的值。 (onChangedListener,onKeyUp)

时间:2014-12-20 05:42:59

标签: android android-edittext textchanged

我正在尝试创建一个edittext字段,其中出现的字母与用户在edittext字段中输入的字母不同。当用户通常在编辑文本中写下来时,它应该对用户显得正常,但是当用户按下"。"时,它应该从定义的字符串中放下字符。当用户按下","时,它将放下已定义字符串中的最后一个字符,并允许用户正常按键进入编辑文本。

存在的两个问题是,当用户初始放下"。"时,用户可以看到"。"在编辑文本被删除之前,并在编辑文本上添加了一个新的字符。这是个问题。 onKeyUp方法首先播放并更改允许键入和替换其余字符的布尔值。它会在onChangedListner的onTextChanged方法上激活。

另一个问题是编辑文本中没有任何内容,用户按下"开始关闭。"开始在编辑文本中创建false字符。它给了我一个拼写检查错误,说明"解析无效区域,从0到1"。我可以为onTextChanged做一个专门检查是否只有"。"在edittext中,用我定义的字符串的第一部分替换它。

我的java代码如下:

public class MainActivity extends Activity {

//Variables
String currentsecret = "";
String totalstring = "";
boolean secretmodeactivated = false;
int stringsum = 0;

boolean firstperiod = true;
boolean settext = true;
boolean deleteboolean = false;

//variables for second try... fuck
boolean periodactivation = false; //activates on . and deactivates with ,
String thedefaultstring = "Android, please answer the following question                     ";
int locationwhereperiodhappened = 0;
boolean haschanged; //used when changing within the textwatcher

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText e_petition = (EditText) findViewById(R.id.petition);
    final EditText e_question = (EditText) findViewById(R.id.question);

    Button b_ask = (Button) findViewById(R.id.ask);
    final TextView t_debug = (TextView) findViewById(R.id.debug);
    final TextView t_debug2 = (TextView) findViewById(R.id.debug2);
    final TextView t_debug3 = (TextView) findViewById(R.id.debug3);
    final TextView t_debug4 = (TextView) findViewById(R.id.debug4);
    final TextView t_debug5 = (TextView) findViewById(R.id.debug5);

    b_ask.setOnClickListener(new OnClickListener() {

        String petitionstring;
        String fuck;

        @Override
        public void onClick(View arg0) {
            //get the string and display it
            petitionstring = e_petition.getText().toString();
            fuck = petitionSplitter(petitionstring);
            t_debug.setText(fuck);
            t_debug2.setText(currentsecret);
            //ASKS



        }
    });

    Button b_reset = (Button) findViewById(R.id.reset);
    b_reset.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //clears everything
             final EditText e_petition = (EditText) findViewById(R.id.petition);
             String currentstring = e_petition.getText().toString();
            nextDefaultStringToPutIn(currentstring);
            currentsecret = "";
            e_petition.setText("");
            e_question.setText("");
            t_debug.setText("debug");
            t_debug2.setText("debug2");
        }
    });
    //edit text on change listener to change the debug texts on keystroke
 //   e_question.addTextChangedListener(watcher)(
    //ISG

    e_petition.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable arg0) {
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            System.out.println("onTextChanged");


            boolean pasttheperiodint = locationwhereperiodhappened <= e_petition.getText().length();

            //if the length of the string is less than when the period got put in, the period must have been deleted...
                // so turn the period activation OFF then.
            if(!pasttheperiodint)
            {
                periodactivation = false;
            }
            //activates
            if(periodactivation && !haschanged && pasttheperiodint)
            {
               haschanged = true;
               final EditText e_petition = (EditText) findViewById(R.id.petition);
                String current = e_petition.getText().toString();
                e_petition.setText(nextDefaultStringToPutIn(current));
                e_petition.setSelection(e_petition.getText().length());
              haschanged = false;
            }

        }

    }            );
}
    //return the char of the string with location of index
//replaces the END of the string with the current char of the default
//lol
///////////////////////////////////////////////
public String removeLastCharInString(String thestring)
{
    if(thestring.isEmpty() && periodactivation)
    {
        return "A";
    }

    if(thestring.isEmpty())
    {
        return "";
    }
    thestring = thestring.substring(0, thestring.length() - 1);


    return thestring;

}
    //if type "1234", replaces the 4 with the char "r" which is from Android, pleas... 4th letter.
    //meant to be used for once period is in action
    public String nextDefaultStringToPutIn(String thestring)
    {
        //removes the last bit and gets the location

        if(thestring.equals("."))
        {
            return "A";
        }
        if(thestring.equals('.'))
        {
            return "A";
        }

        thestring = removeLastCharInString(thestring);
        int thestringlength = thestring.length();
        if(thestringlength == 0)
        {
            return thestring;
        }

        String defaultstring = thedefaultstring.substring(thestringlength, thestringlength + 1);
        System.out.println("!!The string to be printed: " +  defaultstring);
                //thedefaultstring;
        thestring = thestring + defaultstring;
        System.out.println("!!!The string to be returned: " +  thestring);

        return thestring;
    }

    public char currentstring()
    {
        char thecurrentstring = 0;
        thecurrentstring = thedefaultstring.charAt(stringsum);
               // thedefaultstring.charAt(stringsum);

        return thecurrentstring;
    }

    //gives you a string between , and . in a given string
    //not sure what happens if given a string w/o , or .
   public String petitionSplitter(String input)
   {
       if(!input.contains(",") && !input.contains("."))
       {
           //IF your petition doesnt even have a comma AND a period... then
           //should return a string from a given list of strings
           //to confuse those who don't know how it works
               return "You should ask a different question. Put a method that gives you tons of     other messages too";
       }

       if(input.contains(".") && !input.contains(","))
       {
           //IF your petition doesnt even have a comma AND a period... then
           //should return a string from a given list of strings
           //to confuse those who don't know how it works

           input = input.substring(input.indexOf(".") + 1);
           return input;
       }


       input = input.substring(input.indexOf(",") + 1);

       input = input.substring(0, input.indexOf("."));
       return input;
   }


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {


    final EditText e_question = (EditText) findViewById(R.id.question);
    final EditText e_petition = (EditText) findViewById(R.id.petition);

    switch (keyCode) {
        case KeyEvent.KEYCODE_COMMA:
            secretmodeactivated = false;
            periodactivation = false;
            return true;
        case KeyEvent.KEYCODE_SEMICOLON:
            e_question.requestFocus(); 
            //changes focus onto the question box
            return true;
        case KeyEvent.KEYCODE_PERIOD:
            System.out.println("Period Activated");
            secretmodeactivated = true;
            periodactivation = true;

            locationwhereperiodhappened = e_petition.getText().toString().length();

                String current = e_petition.getText().toString();
                e_petition.setText(nextDefaultStringToPutIn(current));
                e_petition.setSelection(e_petition.getText().length());


            firstperiod = false;
            //set int as location of index
            return true;
        case KeyEvent.KEYCODE_DEL:
            deleteboolean = true;
            return true;
        default:
            System.out.println(keyCode);
            return super.onKeyUp(keyCode, event);
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

0 个答案:

没有答案