失去焦点时候搜索图标居中显示,获得焦点的时候搜索图标移动到左侧显示的EditText,同时当EditText不为空时显示一键清除图标,点击清除图标清除内容。
示例代码:
public class MainActivity extends AppCompatActivity {
private IconCenterEditText iconCenterEditText_one ;
private IconCenterEditText iconCenterEditText_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iconCenterEditText_one = (IconCenterEditText) findViewById(R.id.et_search_one);
iconCenterEditText_one.setOnEditTextListener(new IconCenterEditText.OnEditTextListener() {
@Override
public void onEnterKeyAction(View view) {
Toast.makeText(MainActivity.this,"第一個EnterKey被点击了",Toast.LENGTH_LONG).show();
}
@Override
public void onHasFocusAction(View view) {
Toast.makeText(MainActivity.this,"第一個得到焦点",Toast.LENGTH_LONG).show();
}
@Override
public void onLostFocusAction(View view) {
Toast.makeText(MainActivity.this,"第一個失去焦点",Toast.LENGTH_LONG).show();
}
});
iconCenterEditText_two = (IconCenterEditText) findViewById(R.id.et_search_two);
iconCenterEditText_two.setOnEditTextListener(new IconCenterEditText.OnEditTextListener() {
@Override
public void onEnterKeyAction(View view) {
Toast.makeText(MainActivity.this,"第二個EnterKey被点击了",Toast.LENGTH_LONG).show();
}
@Override
public void onHasFocusAction(View view) {
Toast.makeText(MainActivity.this,"第二個得到焦点",Toast.LENGTH_LONG).show();
}
@Override
public void onLostFocusAction(View view) {
Toast.makeText(MainActivity.this,"第二個失去焦点",Toast.LENGTH_LONG).show();
}
});
}
}
附录参考链接
Android自定义View示例(一)—带有删除按钮的EditText
求解答:android Drawable类的方法setBounds(int ,int, int, in ...