listview的edittext如何获得焦点,android 软键盘的显示与隐藏

listview的多个edittext如何分别获得焦点呢?

//listview后获取焦点,优先级低于item

listView.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);

我的思路就是在edittext的头部包层button,监听button的点击事件,来获取焦点

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="10dp" >


        <TextView
            android:id="@+id/tv_faPiaoKaili"
            style="@style/left_title"
            android:text="@string/product_fapiaoKaili" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >


                <RadioButton
                    android:id="@+id/radio0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/product_fapiao3" />


                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/product_fapiao2" />
            </RadioGroup>


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >


                <EditText
                    android:id="@+id/et_code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/aboutus_content_bg003"
                    android:ems="10"
                    android:hint="@string/product_fapiaoCode"
                    android:inputType="phone"
                    android:padding="8dp" />


                <Button
                    android:id="@+id/bt_code"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/transparent"
                    android:focusable="false" 
                    />
            </FrameLayout>



            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >


                <EditText
                    android:id="@+id/et_header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:background="@drawable/aboutus_content_bg003"
                    android:ems="10"
                    android:hint="@string/product_fapiaoHeader"
                    android:inputType="phone"
                    android:padding="8dp" />


                <Button
                    android:id="@+id/bt_header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/transparent"
                    android:focusable="false" 
                    />
            </FrameLayout>

        </LinearLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingTop="10dp" >


        <TextView
            android:id="@+id/tv_payMethod"
            style="@style/left_title"
            android:text="@string/product_payStyle" />


        <Spinner
            android:id="@+id/sp_payMethod"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aboutus_content_bg003" />
    </LinearLayout>


    <TextView
        android:id="@+id/tv_remark"
        style="@style/left_title"
        android:layout_marginTop="10dp"
        android:text="@string/product_remark" />


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <EditText
            android:id="@+id/et_remark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/aboutus_content_bg003"
            android:ems="115"
            android:gravity="left"
            android:minHeight="80dp"
            android:padding="5dp" />


        <Button
            android:id="@+id/bt_remark"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:focusable="false"
            />
    </FrameLayout>

//代码

viewHolder004.bt_code.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

//其它两个edittext先设置不能获取焦点
viewHolder004.et_header.setFocusable(false);
viewHolder004.et_remark.setFocusable(false);

showSoftInput(viewHolder004.et_code);
}
});
viewHolder004.bt_header.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
viewHolder004.et_code.setFocusable(false);
viewHolder004.et_remark.setFocusable(false);
showSoftInput(viewHolder004.et_header);
}
});
viewHolder004.bt_remark.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
viewHolder004.et_header.setFocusable(false);
viewHolder004.et_code.setFocusable(false);
                  showSoftInput(viewHolder004.et_remark);
}
});

/**
* @param editText  系统键盘的弹出
*/
private void showSoftInput(EditText editText) {

//3个一起设置才有效,不然可能无效
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();

InputMethodManager inputMethodManager=(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//系统键盘显示后=>隐藏的切换
        //inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, //InputMethodManager.HIDE_NOT_ALWAYS);
//没用
//inputMethodManager.showSoftInputFromInputMethod(viewHolder004.et_code.getWindowToken(), //InputMethodManager.SHOW_IMPLICIT);
//显示系统键盘
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}

/**隐藏软键盘
* @param context
* @param et_tel
*/
public static void hiddenSoftInput(Context context, EditText et_tel) {
// 隐藏键盘
InputMethodManager imm = (InputMethodManager) context
.getApplicationContext().getSystemService(
Context.INPUT_METHOD_SERVICE);


// 如果输入法在窗口上已经显示,则隐藏,反之则显示
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
// 强制隐藏键盘
if (et_tel != null) {
IBinder applicationWindowToken = et_tel.getApplicationWindowToken();
if(applicationWindowToken!=null){
imm.hideSoftInputFromWindow(applicationWindowToken, 0);
}
}
}
/**
* @param context   隐藏软键盘
*/
public static void hiddenSoftInput(Context context)
{
if(context  instanceof  Activity){
// 隐藏键盘
InputMethodManager imm = (InputMethodManager) context
.getApplicationContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
View currentFocus = ((Activity) context).getCurrentFocus();
if(currentFocus!=null){
IBinder applicationWindowToken = currentFocus.getApplicationWindowToken();
if(applicationWindowToken!=null){
imm.hideSoftInputFromWindow(applicationWindowToken, 0);
}
}
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值