Focus - Android时写入任何Edittext

时间:2014-03-29 08:54:13

标签: android focus android-edittext calculator

我有一个简单的计算器应用程序。一个由按钮组成的小键盘,我在一个edittext上显示结果。现在我想做的是拥有3个edittext,意味着3个结果。所以当我点击/焦点对任何一个edittext时。我可以打字。在小键盘上并将其发布到3个edittext中的任何一个。它现在适用于一个edittext,如何使它适用于所有的edittext?这是我的代码

public class MainActivity extends Activity {

public String str ="";

 Character op = 'q';
 int i,num,numtemp;
    EditText showResult;
    String displayStr = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        showResult = (EditText)findViewById(R.id.result_id);
        showResult1 = (EditText)findViewById(R.id.result_id1);
        showResult2 = (EditText)findViewById(R.id.result_id2);


    }
      public void btn1Clicked(View v){
       insert(1);

      }

 public void btn2Clicked(View v){
       insert(2);

      }
      public void btn3Clicked(View v){
       insert(3);

      }
      public void btn4Clicked(View v){
       insert(4);

      }
      public void btn5Clicked(View v){
       insert(5);

      }
      public void btn6Clicked(View v){
       insert(6);
      }
      public void btn7Clicked(View v){
       insert(7);

      }
      public void btn8Clicked(View v){
       insert(8);

      }
      public void btn9Clicked(View v){
       insert(9);

      }

      public void btn0Clicked(View v){
           insert(0);

          }


      public void btnplusClicked(View v){
       perform();
       op = '+';
       displayStr += "+";
       showResult.setText(displayStr);
      }

 public void btnminusClicked(View v){
     perform();
        op = '-';
        displayStr += "-";    
        showResult.setText(displayStr);
      }
      public void btndivideClicked(View v){
       perform();
       op = '/';
       displayStr += "/";
       showResult.setText(displayStr);
      }
      public void btnmultiClicked(View v){
       perform();
       op = '*';
       displayStr += "*";
       showResult.setText(displayStr);
      }
      public void btnequalClicked(View v){



          calculate();

      }

 public void btnclearClicked(View v){
       reset();
      }
      private void reset() {
  // TODO Auto-generated method stub
   str ="";
   op ='q';
   num = 0;
   numtemp = 0;
   displayStr = "";
   showResult.setText("");
 }

 private void insert(int j) {
    // TODO Auto-generated method stub
       str = str+Integer.toString(j);
       num = Integer.valueOf(str).intValue();        
       displayStr += Integer.toString(j); 
       showResult.setText(displayStr);

   }
      private void perform() {
    // TODO Auto-generated method stub
    str = "";
    numtemp = num;
   }




      private void calculate() {


          if(op == '+')
     num = numtemp+num;
    else if(op == '-')
     num = numtemp-num;
    else if(op == '/')
     num = numtemp/num;
    else if(op == '*')
     num = numtemp*num;


showResult.setText(""+num);

   }

}

3 个答案:

答案 0 :(得分:0)

试试这个。希望它会对你有所帮助。

编程方式:

edittext.requestFocus();

通过xml:

<EditText...>
    android:focusableInTouchMode="true"
    <requestFocus />
</EditText>

或手动调用onClick方法。

答案 1 :(得分:0)

试试这个..

为每EditText

执行此操作
showResult.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {

            showResult.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(showResult, InputMethodManager.SHOW_IMPLICIT);
        }
    });

答案 2 :(得分:0)

老兄,这个问题与逻辑有关,而不是机器人问题 但 您可以使用变量

int i=0;

when first edittext is clicked just changed to i=1
second edittext is clicked just changed to i=2
thirdedittext is clicked just changed to i=3
so when result button is clicked just check the value of i 
and print the result in respective edittext