取出SearchIcon的提示在搜索查看 [英] Remove the SearchIcon as hint in the searchView
问题描述
我在做一个应用程序,例如,当我点击的形象(这是一个搜索查看)
搜索垫打开!
和它看起来像
但这里的默认搜索图标(放大镜)被显示,但只要一些文本,进入此dissappears
但我不想,即使画面被点击,第一次被显示放大镜
在这里我没有使用任何XML文件
我的code是
公共类MainActivity延伸活动{
@燮pressLint(NewApi)
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
RelativeLayout的相对=新RelativeLayout的(这一点);
的LayoutParams PARAMS =新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
relative.setLayoutParams(PARAMS);
的setContentView(相对);
搜索查看搜索查看=新的搜索查看(本);
traverseView(搜索查看,0);
// searchView.setIconifiedByDefault(假);
的LayoutParams searchViewparams =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
// searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
searchView.setLayoutParams(searchViewparams);
relative.addView(搜索查看);
}
@TargetApi(Build.VERSION_ codeS.HONEYCOMB)
@燮pressLint(NewApi)
私人无效traverseView(查看视图,INT指数){
如果(查看的instanceof搜索查看){
搜索查看V =(搜索查看)视图;
的for(int i = 0; I< v.getChildCount();我++){
traverseView(v.getChildAt(ⅰ),ⅰ);
}
}否则,如果(查看的instanceof的LinearLayout){
的LinearLayout LL =(LinearLayout中)查看;
的for(int i = 0; I< ll.getChildCount();我++){
traverseView(ll.getChildAt(ⅰ),ⅰ);
}
}否则,如果(查看的instanceof的EditText){
((EditText上)查看).setTextColor(Color.GREEN);
((EditText上)查看).setHintTextColor(Color.BLACK);
}否则,如果(查看的instanceof的TextView){
((TextView中)查看).setTextColor(Color.BLUE);
}否则,如果(查看的instanceof ImageView的){
((ImageView的)视图).setImageResource(R.drawable.ic_launcher);
} 其他 {
Log.v(观侦察兵,未定义视图类型在这里......);
}
}
}
我已经受够了这种麻烦了。我结合我发现教程和现有的应答发现这里的计算器。
INT马吉德= getResources()则getIdentifier(机器人:ID / search_mag_icon,NULL,NULL);。
ImageView的magImage =(ImageView的)searchView.findViewById(马吉德);
magImage.setLayoutParams(新LinearLayout.LayoutParams(0,0));
注意到,搜索查看是一个LinearLayout中,所以使用LinearLayout.LayoutParams以避免例外。
我也试过,但它不会删除该视图。我似乎无法弄清楚原因:
magImage.setVisibility(View.GONE);
有关,你需要改变对方的观点,请参考本教程。
I am doing an application where for example when i click on the image(it is a searchView)
the search pad opens !
and it looks like
but here the default search icon (magnifier) gets displayed but this dissappears as soon as some text is entered
but i dont want that magnifier to be displayed even the image is clicked for the first time
and here i am not using any xml file
my code is
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout relative = new RelativeLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
relative.setLayoutParams(params);
setContentView(relative);
SearchView searchView = new SearchView(this);
traverseView(searchView, 0);
// searchView.setIconifiedByDefault(false);
LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
// searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
searchView.setLayoutParams(searchViewparams);
relative.addView(searchView);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private void traverseView(View view, int index) {
if (view instanceof SearchView) {
SearchView v = (SearchView) view;
for(int i = 0; i < v.getChildCount(); i++) {
traverseView(v.getChildAt(i), i);
}
} else if (view instanceof LinearLayout) {
LinearLayout ll = (LinearLayout) view;
for(int i = 0; i < ll.getChildCount(); i++) {
traverseView(ll.getChildAt(i), i);
}
} else if (view instanceof EditText) {
((EditText) view).setTextColor(Color.GREEN);
((EditText) view).setHintTextColor(Color.BLACK);
} else if (view instanceof TextView) {
((TextView) view).setTextColor(Color.BLUE);
} else if (view instanceof ImageView) {
((ImageView) view).setImageResource(R.drawable.ic_launcher);
} else {
Log.v("View Scout", "Undefined view type here...");
}
}
}
I've had trouble with this too. I combined the tutorial I found and an existing answer found here in stackoverflow.
int magId = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView magImage = (ImageView) searchView.findViewById(magId);
magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
Take note that searchView is a LinearLayout, so use LinearLayout.LayoutParams to avoid an exception.
I also tried this but it doesn't remove the view. I can't seem to figure why.:
magImage.setVisibility(View.GONE);
For the other views that you need to change, refer to this tutorial.
这篇关于取出SearchIcon的提示在搜索查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!