如何让edittext在activity起始的时候不自动获得焦点

 我来答
你猜我猜哇擦猜
推荐于2016-01-28 · TA获得超过2.8万个赞
知道大有可为答主
回答量:2万
采纳率:0%
帮助的人:6764万
展开全部
在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好。所以一般会通过代码控制让EditText不获取焦点。常用的方式如下,在Activity的布局文件中加上如下代码:
<!-- 输入焦点控制 -->

<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
在日前开发的一个项目中界面中除了EditText就是Spinner 和Button等空间,EditText 获取焦点以后,点击Spinner总是无法移除焦点:采取如下方式解决,
当点击Spinner时让EditText失去焦点即可:(见标红代码)
// 初始化两个LayoutInflater对象
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

et_name = (EditText) findViewById(R.id.et_name);
ib_add = (ImageButton) findViewById(R.id.ib_add);
ib_clear = (Button) findViewById(R.id.ib_clear);
ib_query = (Button) findViewById(R.id.ib_query);
ib_down = (Button) findViewById(R.id.ib_down);
ib_save = (Button) findViewById(R.id.ib_save);
ib_back = (ImageButton) findViewById(R.id.ib_back);
// ib_sex = (ImageButton) findViewById(R.id.ib_sex) ;

ib_add.setOnClickListener(this);
ib_clear.setOnClickListener(this);
ib_query.setOnClickListener(this);
ib_down.setOnClickListener(this);
ib_save.setOnClickListener(this);
ib_back.setOnClickListener(this);
// ib_sex.setOnClickListener(this) ;

// 性别
ArrayAdapter<String> sexAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
sexAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String sexString;
sexAdapter.clear();
int position = 0;
for (int i = 0; i < sexList.length; i++) {
sexString = sexList[i];
sexAdapter.add(sexString);
}
sp_sex = (Spinner) findViewById(R.id.sp_sex);
sp_sex.setAdapter(sexAdapter);
sp_sex.setFocusable(true);
sp_sex.setSelection(position);
sp_sex.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_sex.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_month.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
et_name.clearFocus();
return false;
}
});
sp_customerlevel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

// 出生月份
ArrayAdapter<String> monthAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String monthString;
monthAdapter.clear();
position = 0;
for (int i = 0; i < monthList.length; i++) {
monthString = monthList[i];
monthAdapter.add(monthString);
}
sp_month = (Spinner) findViewById(R.id.sp_month);
sp_month.setAdapter(monthAdapter);
sp_month.setFocusable(true);
sp_month.setSelection(position);
sp_month.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

// 客户等级
ArrayAdapter<String> customerlevelAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
customerlevelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String customerlevelString;
customerlevelAdapter.clear();
position = 0;
for (int i = 0; i < customerlevelList.length; i++) {
customerlevelString = customerlevelList[i];
customerlevelAdapter.add(customerlevelString);
}
sp_customerlevel = (Spinner) findViewById(R.id.sp_customerlevel);
sp_customerlevel.setAdapter(customerlevelAdapter);
sp_customerlevel.setFocusable(true);
sp_customerlevel.setSelection(position);
sp_customerlevel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView v1 = (TextView) view;
if (v1 != null)
v1.setTextColor(Color.BLACK); // 可以随意设置自己要的颜色值
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式