怎么让焦点显示在数字的右边

图片说明
图片说明

 package com.example.calculate;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Calculate extends Activity implements OnClickListener {
    Button btn0;
    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    Button btn6;
    Button btn7;
    Button btn8;
    Button btn9;
    Button btnAdd;
    Button btnSub;
    Button btnPoint;
    Button btnMul;
    Button btnDiv;
    Button btnEqu;
    Button btnCE;
    EditText editText;
    String Symbol;
    double firnum;
    double secnum;
    double res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        btn0=(Button)findViewById(R.id.btn0);
        btn1=(Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        btn3=(Button)findViewById(R.id.btn3);
        btn4=(Button)findViewById(R.id.btn4);
        btn5=(Button)findViewById(R.id.btn5);
        btn6=(Button)findViewById(R.id.btn6);
        btn7=(Button)findViewById(R.id.btn7);
        btn8=(Button)findViewById(R.id.btn8);
        btn9=(Button)findViewById(R.id.btn9);
        btnSub=(Button)findViewById(R.id.btnSub);
        btnAdd=(Button)findViewById(R.id.btnAdd);
;       btnPoint=(Button)findViewById(R.id.btnPoint);
        btnMul=(Button)findViewById(R.id.btnMul);
        btnDiv=(Button)findViewById(R.id.btnDiv);
        btnEqu=(Button)findViewById(R.id.btnEqu);
        btnCE=(Button)findViewById(R.id.btnCE);
        editText=(EditText)findViewById(R.id.editText);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        btnDiv.setOnClickListener(this);
        btnPoint.setOnClickListener(this);
        btnSub.setOnClickListener(this);
        btnMul.setOnClickListener(this);
        btnEqu.setOnClickListener(this);
        btnCE.setOnClickListener(this);
    }

     public void onClick(View v){
           switch(v.getId()){
               case R.id.btn0:
                   editText.setText(editText.getText()+"0");
                   break;
               case R.id.btn1:
                   editText.setText(editText.getText()+"1");
                   break;
               case R.id.btn2:
                   editText.setText(editText.getText()+"2");
                   break;
               case  R.id.btn3:
                   editText.setText(editText.getText()+"3");
                   break;
               case R.id.btn4:
                   editText.setText(editText.getText()+"4");
                   break;
               case R.id.btn5:
                   editText.setText(editText.getText()+"5");
                   break;
               case R.id.btn6:
                   editText.setText(editText.getText()+"6");
                   break;
               case R.id.btn7:
                   editText.setText(editText.getText()+"7");
                   break;
               case R.id.btn8:
                   editText.setText(editText.getText()+"8");
                   break;
               case R.id.btn9:
                   editText.setText(editText.getText()+"9");
                   break;
               case R.id.btnPoint:
                   editText.setText(editText.getText()+".");
                   break;
               case R.id.btnAdd:
                   Symbol="+";
                   firnum=Double.valueOf(editText.getText().toString());
                   editText.setText("");
                   break;
               case R.id.btnSub:
                   Symbol="-";
                   firnum= Double.valueOf(editText.getText().toString());
                   editText.setText("");
                   break;
               case R.id.btnMul:
                   Symbol="*";
                   firnum=Double.valueOf(editText.getText().toString());
                   editText.setText("");
                   break;
               case R.id.btnDiv:
                   Symbol="/";
                   firnum= Double.valueOf(editText.getText().toString());
                   editText.setText("");
                   break;
               case R.id.btnCE:
                   editText.setText("");
                   break;
               case R.id.btnEqu:
                   secnum=Double.valueOf(editText.getText().toString());
                   if (Symbol=="+"){
                       res=firnum+secnum;
                   }
                   if (Symbol=="-"){
                       res=firnum-secnum;
                   }
                   if (Symbol=="*") {
                       res = firnum * secnum;
                   }
                   if (Symbol=="/") {
                       res = firnum / secnum;
                   }

                   editText.setText(res+" ");
           }
           }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

3个回答

editText.setText(res+" ");
editText.setSelection(editText.getText().length()); //加上这句
YXTS122
YXTS122 回复Tiger_Zhao:应该还要在每个数字键的点击事件下加上你这一句
大约 3 年之前 回复

在xml文件EditText下增加:

 android:textDirection="rtl"

如果你是要从右到左输入的话 就使用楼上的方法 如果你是单纯想移动光标的话如下
EditText edt = (EditText)findViewById(R.id.inbox);
edt.setSelection(edt.getText().length());

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问