在我的登录活动中,我添加了一个为编辑文本设置边框的方法(如果它是可聚焦的),以便用户可以知道他正在此字段中输入数据并且边框将突出显示...但是当活动开始,该方法直接应用于第一个用户名编辑文本知道我正在隐藏键盘...我希望这个方法在键盘出现之前工作。谢谢你帮助我...这是我的代码
爪哇
public class FoodSafetyLogIn extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_safety_log_in);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.t1_layout);
//Hide Keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
EditText username = (EditText) findViewById(R.id.input_username);
focus(username);
EditText password = (EditText) findViewById(R.id.input_password);
focus(password);
password.setTypeface(Typeface.DEFAULT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_back) {
Intent i = new Intent(FoodSafetyLogIn.this, MainActivity.class);
startActivity(i);
overridePendingTransition(R.animator.slide_in_right,R.animator.slide_in_right);
return true;
}
return super.onOptionsItemSelected(item);
}
public void OnClick(View view){
}
public void focus(View view){
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setBackgroundResource(R.drawable.focus_border_style);
else
v.setBackgroundResource(R.color.colorPrimary);
}
});
}
}
答案 0 :(得分:1)
这是一个例子:
<LinearLayout
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
your edit text... />
答案 1 :(得分:0)
在EditView中添加此标记
android:focusableInTouchMode="true"