软键盘未显示在自定义对话框片段中

时间:2013-10-03 06:33:40

标签: android dialog fragment

我有自定义对话框片段,其中包含视图寻呼机,在视图中,寻呼机有一个编辑文本。当显示自定义对话框片段时,编辑文本具有焦点但软键盘未显示输入。

对话框的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:htdu87="http://schemas.android.com/apk/res/com.htdu87.ekaraoke"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/background_app" >

    <!-- <com.htdu87.ekaraoke.objects.LikeComment 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/htdu87_like_comment"/> -->

    <com.htdu87.ekaraoke.classes.MyViewPager 
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/view_pager"
        htdu87:allow_sweep="true"/>

</LinearLayout>

java代码

package com.htdu87.ekaraoke.classes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.htdu87.ekaraoke.R;
import com.htdu87.ekaraoke.adapters.ViewPagerAdapter;
import com.htdu87.ekaraoke.configs.AppConfig;

public class LikeCommentDialogFragment extends DialogFragment {
    private long postId;
    private final String COMMENT = "Comment";
    private final String LIKE = "Like";
    private ListView lstComment;
    private ListView lstLike;
    private ImageButton btnPos;
    private EditText txtComment;
    private ArrayAdapter<String> adapterComment;

    public LikeCommentDialogFragment(){

    }


    public void setPostId(long id){
        postId = id;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View likeCommentDlg = inflater.inflate(R.layout.like_comment_dialog, null); 

        MyViewPager viewPager = (MyViewPager)likeCommentDlg.findViewById(R.id.view_pager);

        MyFragment frg = new MyFragment();
        frg.setViewResourceId(R.layout.comment);
        getActivity().getSupportFragmentManager().beginTransaction().add(frg, COMMENT).commit();

        frg = new MyFragment();
        frg.setViewResourceId(R.layout.like);
        getActivity().getSupportFragmentManager().beginTransaction().add(frg, LIKE).commit();

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getActivity(), getActivity().getSupportFragmentManager(), new String[]{COMMENT, LIKE});
        viewPager.setAdapter(pagerAdapter);

        new LoadComment().execute();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(likeCommentDlg);
        Dialog d = builder.create();
        d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        d.setCanceledOnTouchOutside(false);
        return d;
    }

    private class LoadComment extends AsyncTask<Void, Void, Integer>{
        private List<String> data;

        @Override
        protected Integer doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            String url = AppConfig.SERVER_URL + "load_comment.php?postid="+postId+"&tokent="+AppConfig.TOKENT;
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if(httpResponse.getStatusLine().getStatusCode() == 200){
                    if(!httpResponse.getFirstHeader("Content-Type").getValue().equals("application/json; charset=utf8"))
                        return -1;
                    HttpEntity httpEntity = httpResponse.getEntity();
                    JSONObject jsonObj = new JSONObject(EntityUtils.toString(httpEntity));
                    JSONArray comments = jsonObj.getJSONArray("comments");
                    int count = comments.length();
                    data = new ArrayList<String>();
                    for(int i = 0; i < count; i++){
                        JSONObject commentObj = comments.getJSONObject(i);
                        JSONObject comment = commentObj.getJSONObject("comment");
                        data.add(comment.getString("commentcontent"));
                    }
                    return 0;
                }
                return -2;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return -3;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            View v = getActivity().getSupportFragmentManager().findFragmentByTag(COMMENT).getView();
            lstComment = (ListView)v.findViewById(R.id.lst_comment);
            txtComment = (EditText)v.findViewById(R.id.txt_comment);

            btnPos = (ImageButton)v.findViewById(R.id.btn_post_comment);
            //btnPos.setOnClickListener(LikeComment.this);
            LinearLayout layLoading = (LinearLayout)v.findViewById(R.id.lay_loading);

            adapterComment = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);
            lstComment.setAdapter(adapterComment);
            layLoading.setVisibility(View.GONE);

            txtComment.requestFocus();
            //InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            //imm.showSoftInput(txtComment, 0);
        }
    }
}

xml containt编辑文本

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lst_comment"
        android:layout_above="@+id/lay_write_comment"/>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_marginTop="5dp"
        android:id="@+id/lay_write_comment">

        <EditText 
            android:layout_width="0dp"
            android:layout_weight="8"
            android:hint="@string/str_your_commnet"
            android:layout_height="match_parent"
            android:padding="5dp"
            android:id="@+id/txt_comment"
            android:background="#ffffff"/>

        <ImageButton 
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_button_login"
            android:src="@drawable/ic_menu_send"
            android:contentDescription="@string/str_post_comment"
            android:layout_marginLeft="5dp"
            android:id="@+id/btn_post_comment"/>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lay_loading"
        android:background="@color/background_app"
        android:orientation="horizontal"
        android:gravity="center">

        <ProgressBar 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/progressBarStyleSmall"/>

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_load_comment"
            android:layout_marginLeft="5dp"
            android:textSize="12sp"/>

    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

也许这个答案,ListView而不是ViewPager可以帮到你?

Soft Keyboard Doesnt show when using adapter in DialogFragment

答案 1 :(得分:0)

确保您没有使用android:inputType="none"的编辑文字 并且没有在清单android:windowSoftInputMode="stateAlwaysHidden"

中使用它