我有一个实现的滑动菜单片段,我在其中使用的EditText遇到问题。不幸的是,每当我在滑动菜单中的EditText中键入内容时,提示仍会显示。因此,我试图找到一种解决方法,因此,如果EditText中没有任何内容,我基本上将提示设置为不显示任何提示,但这根本不起作用。但是,另一个基于文本启用或禁用按钮的代码确实可以工作。对我来说是个谜。
它在做什么:
http://i.stack.imgur.com/9DEPN.png
我的解决方法:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dbHandler = new DatabaseHandler(getActivity());
final Button addBtn = (Button) getView().findViewById(R.id.btnAddPlaylist);
final EditText txtPlaylistName = (EditText) getView().findViewById(R.id.txtPlaylist);
txtPlaylistName.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) {
boolean hasContent = !String.valueOf(s).trim().isEmpty();
addBtn.setEnabled(hasContent);
txtPlaylistName.setHint((hasContent) ? "Create new Playlist..." : "");
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
如果您的EditText为空,则必须使用字符串调用setHint
,而不是包含文本的话。在您的代码段中,您有:
txtPlaylistName.setHint((hasContent) ? "Create new Playlist..." : "");
这甚至暗示您仍然有一些内容,
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
boolean hasContent = (count != 0);
addBtn.setEnabled(hasContent);
txtPlaylistName.setHint((hasContent) ? null : "Create new Playlist...");
}
EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");
If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:
TextInputLayout layoutUser;
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname);
layoutUser.setHint(getResources().getString(R.string.hintFaculty));
您可以将提示添加到布局容器,它将按预期显示。这是为动态创建的EditText添加提示的方法。希望有人能从中得到受益。
TextInputLayout textInputLayout = holder.itemViewfindViewById(R.id.value_text);
textInputLayout.setHint(your_value.getTitle());