简体   繁体   English

Android:EditText

[英]Android: EditText

I have a listview containing a list of mathematical functions such as sin, cos, tan. 我有一个listview其中包含诸如sin,cos,tan等数学函数的列表。 I am trying to output the selected item in the listview and output it in a EditText . 我试图在listview输出选定的项目,并在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. 当我使用下面的代码时,可以在EditText看到所选的项目,但是当我想在括号中添加其他数字时,它不在括号之间。 How should i go about correcting this? 我应该如何纠正呢?

Example i selected the round function, it should appear Round() in the EditText like Round(2.3). 例如,我选择了round函数,它应该在EditText显示Round() ,如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. 它就像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 另外,函数updateEditText()无效,因为每次调用该函数时都会创建stringbuilder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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