各类App都有自己的EditText方式,本人认为友好型EditText进入页面时没有键盘弹出框,并且触发方式是Touch而不是Click方式。
上代码:
activity_main.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="15dp"
android:src="@drawable/search" />
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@null"
android:hint="搜索"
android:maxLength="15"
android:textColor="@color/color_333"
android:textSize="15sp" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText search;//搜索
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
void init() {
search = (EditText) findViewById(R.id.search);
search.setFocusable(false);
search.setFocusableInTouchMode(false);
search.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//显示键盘
search.setFocusable(true);
search.setFocusableInTouchMode(true);
return false;
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
//搜索数据改变时处理
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
search.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
//键盘删除键监听操作
}
return false;
}
});
}
}
search.png: