Can you make an EditText input from right to left?

2019-01-27 19:54发布

I was wondering if you can control input on an EditText to move from right to left? The EditText would have to support insert/delete etc. from right to left as well. Is there a way to do this?

6条回答
别忘想泡老子
2楼-- · 2019-01-27 20:25

Add this to your editText

        android:textDirection="rtl"
查看更多
我命由我不由天
3楼-- · 2019-01-27 20:27

There is the manual way to do it by using the following code :

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

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Each time click Menu button on the bar
        // Set the cursor to the first index
        mEtEmail.setSelection(0); // YOUR EDIT TEXT
    } 

    return super.onKeyUp(keyCode, event);
}

p/s : Or you can change

KeyEvent.KEYCODE_MENU

follow your wishes,

Or you can try with Bidi.

Thanks,

查看更多
不美不萌又怎样
4楼-- · 2019-01-27 20:29

Try This....

  1. Initialize User name and password fields.

    EditText username = (EditText) findViewById(R.id.username);
    
    EditText password = (EditText) findViewById(R.id.password);
    
  2. Get current Locale for RTL languages like Arabic etc,.

    String getCurrentLocale = Locale.getDefault().getDisplayLanguage();
    
  3. Then we should check which language is selected for RTL or LTR.

    if(getCurrentLocale.equalEgnoreCase("English")){          //LTR languages
    
          username.setGravity(Gravity.Left);
    
          password.setGravity(Gravity.Left);
    
     }else{                                                     //RTL languages
    
         username.setGravity(Gravity.Right);
    
         password.setGravity(Gravity.Right);
    
     }
    
  4. Happy coding...!

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-27 20:47

Just use this:

android:layout_gravity="start"
android:textAlignment="viewStart"
查看更多
混吃等死
6楼-- · 2019-01-27 20:48

It sounds like you just need to set gravity to the right.

查看更多
爷、活的狠高调
7楼-- · 2019-01-27 20:49

try this code

//initialize
EditText userName = (EditText)findViewById(R.id.userName);

//set gravity for userName 
userName.setGravity(Gravity.RIGHT);
查看更多
登录 后发表回答