问题描述
我是一个Android和iPhone的开发。我是一个新手,但在做这个浩大的工程。我想实现的leftView,leftviewmode,rightview,在iPhone在Android上的文本框的rightviewmode属性。
I am an android and iphone developer. I am a newbie though working on this huge project. I am trying to implement the leftView, leftviewmode,rightview,rightviewmode properties of the textfield in iphone in android.
我在想,随着leftview和rightview的setCompoundDrawables()方法可以迎接挑战。不过,我难倒就如何落实各种模式; 从不,总是等。
I was thinking that with the leftview and rightview the setCompoundDrawables() method could meet the challenge. However, I am stumped as to how to implement the various modes; "Never", "always" etc.
我是在正确的轨道?任何人都可以请帮助?
Am I on the right track? Could anyone please help?
感谢
推荐答案
这code会给你一个Android的EditText字段功能酷似iPhone的UITextField与clearbuttonmode,leftview,rightview,leftviewmode,rightviewmode,UITextFieldViewModeNever选项,UITextFieldViewModeWhileEditing,UITextFieldViewModeUnlessEditing,UITextFieldViewModeAlways
This code will give you an Android EditText field that functions exactly like an iPhone UITextField with options for clearbuttonmode, leftview, rightview, leftviewmode, rightviewmode, UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways
String value = "";//any text you are pre-filling in the EditText
final String viewMode = "editing";//never | editing | unlessEditing | always
final String viewSide = "right"; //left | right
final EditText et = new EditText(this);
et.setText(value);
//your leftview, rightview or clearbuttonmode image. for clearbuttonmode this one from standard android images looks pretty good actually
final Drawable x = getResources().getDrawable(R.drawable.presence_offline);
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
Drawable x2 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?(value.equals("") ? null : x):viewMode.equals("unlessEditing")?(value.equals("") ? x : null):null;
et.setCompoundDrawables(viewSide.equals("left")?x2:null, null, viewSide.equals("right")?x2:null, null);
et.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (et.getCompoundDrawables()[viewSide.equals("left")?0:2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
//x pressed
if ((viewSide.equals("left")&&event.getX() < et.getPaddingLeft() + x.getIntrinsicWidth())
||(viewSide.equals("right")&&event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth())) {
Drawable x3 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?null:viewMode.equals("unlessEditing")?x:null;
et.setText("");
et.setCompoundDrawables(viewSide.equals("left")?x3:null, null, viewSide.equals("right")?x3:null, null);
}
return false;
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Drawable x4 = viewMode.equals("never")?null:viewMode.equals("always")?x:viewMode.equals("editing")?(et.getText().toString().equals("") ? null : x):viewMode.equals("unlessEditing")?(et.getText().toString().equals("") ? x : null):null;
et.setCompoundDrawables(viewSide.equals("left")?x4:null, null, viewSide.equals("right")?x4:null, null);
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
这篇关于实现机器人的UITextField属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!