简体   繁体   中英

Android: EditText

I have a listview containing a list of mathematical functions such as sin, cos, tan. I am trying to output the selected item in the listview and output it in a EditText . When i used the codes below, the selected item can be seen in the EditText but when i want to add in additional numbers in the bracket it does not goes between the brackets. How should i go about correcting this?

Example i selected the round function, it should appear Round() in the EditText like Round(2.3).

But I could not add in additional numbers in between the bracket. It just appear behind the bracket like this Round()2.3.

Please advice.

My codes are as follows:

     eqString = new StringBuilder();
        ArrayList<String> mathsFuncArray =  new ArrayList<String>();
                mathsFuncArray.add("round");
                mathsFuncArray.add("abs");
                mathsFuncArray.add("sqr");
                mathsFuncArray.add("sqrt");
                mathsFuncArray.add("log");
                mathsFuncArray.add("exp");
                mathsFuncArray.add("sin");
                mathsFuncArray.add("cos");
                mathsFuncArray.add("tan");
                mathsFuncArray.add("cotan");
                ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mathsFuncArray);
                mathsFuncArray.setAdapter(adapter);

                mathsFuncArray.setOnItemClickListener(new OnItemClickListener() {
                      public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
                        String selectedFromList =(String) (mathsFuncArray.getItemAtPosition(myItemInt));
                        eqString.append(selectedFromList + ""()");


                           updateEditText();
                      }                 
                }); 



     private void updateEditText() {

              StringBuilder builder = new StringBuilder();

              builder.append(eqString.toString());

             EditText.setText(builder);
             }

它应该是

eqString.append( "(" +selectedFromList + ")");

Where are you getting the numbers?

this line:

eqString.append(selectedFromList + ""()");

can be modified as such:

Num string could be set up like so:

int num1 = 1;
int num2 = 2;
String numString = ""+num1+","+num2;
eqString.append(selectedFromList + "("+numString+")");

Which would put your numbers string in between the parens.

EDIT:

SInce the numbers are coming from the buttons do this: // global vars:

int selNum1=0;
int selNum2=0;
int mod = 0;
String selItem="";


/// set your arrylistener as before....

 mathsFuncArray.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
                    selItem =(String) (mathsFuncArray.getItemAtPosition(myItemInt));
                    eqString.append(selItem +"()");


                       updateEditText();
                  }                 
            }); 

// set up the button, named Button number1;

 // you'll want to define click listeners for your number buttons.  
 number1.setOnClickListener(new onClickListener({

@Override 
onClick(View v) {
   setSelNums(1);
   String display = selItem+"("+selNum1+","+selNum2+")";
   eqString.setLength(0);
   eqString.append(display);
   updateEditText();

});

public void setSelNums(int num) {
   // you'll have to determine which numbers should be selNum1 or selNum2 and when (i.e. should it just always go "selNum1" is set first, followed by "selNum2" and then new entries just overwrite these...or something else?
    if(mod%2 ==0) {
      selNum1 = num;
    } else {
      selNum2 = num;
    } 
    mod+=1;
}
 eqString.append(selectedFromList + ""()");

You are not adding anything between the ().

eqString.append(selectedFromList + "(" + something + ")");

Also, the function updateEditText() is useless since the stringbuilder is being created everytime you call the function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM