每当要选择autoCompleteteTextView下拉菜单中的元素时,我都想隐藏键盘。
我已经编写了这段代码,并且没有任何错误,但是真正发生的是每当我单击autoCompleteteTextView时,由于onClick函数中的hideSofInputFromWindow,键盘瞬间就进出了。这是Java代码:
package random.one.com.Perfecto;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private AutoCompleteTextView textView1;
private InterstitialAd mInterstitialAd;
private static final String TAG = "MainActivity";
private AdView mAdView;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.loadAd(new AdRequest.Builder().build());
final ImageView toolbar1 = (ImageView) findViewById(R.id.Toolbar1);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
toolbar1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
vp_pages.setAdapter(pagerAdapter);
TabLayout tbl_pages= (TabLayout) findViewById(R.id.tbl_pages);
tbl_pages.setupWithViewPager(vp_pages);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.Loc_names));
textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
textView1.setAdapter(arrayAdapter);
textView1.setThreshold(1);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
textView1.showDropDown();
hideKeyBoard(view);
String city = arrayAdapter.getItem(3);
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText(city);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("ConstantConditions")
public void hideKeyBoard(View view){
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Home) {
// Handle the camera action
} else if (id == R.id.nav_Route) {
} else if (id == R.id.nav_Map) {
} else if (id == R.id.nav_Share) {
} else if (id == R.id.nav_About) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
class FragmentAdapter extends FragmentPagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new Metro();
case 1:
return new Bus();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
//
//Your tab titles
//
case 0:
return "Metro";
case 1:
return "Bus";
default:
return null;
}
}
}
}
因此,有什么方法可以仅在单击下拉菜单项时隐藏键盘。
以及如何通过此方法获得单击(位置)的项目。
最佳答案
您需要将以下行移动到下面的代码,请参见注释:
hideKeyBoard(view);
-在下拉菜单中隐藏键盘:
textView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
hideKeyBoard(view);//move here
String selection = (String) parent.getItemAtPosition(position);//get item selected
}
});
-您可以从上方onItemClick的位置获取单击哪个项目。
关于android - HideSoftKeyboard在onClickListener中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45390867/