android 开发-点击EditText 弹出软键盘的问题

2015-5-11 10:37:14     人阅读    

 

如题,当我点击/悬浮 EditText 文本框的时候 ,我想弹出android软键盘,但是我的app老是实现不了,我看了很多其他人的相似问答,但是答案都不能解决我的问题,我添加了好多的代码片段,但是没有一个起作用的.

MainActivity.java:

public class MainActivity extends ActionBarActivity {



    private boolean mIsPlaying;
 
    private int primesLE;
 
    private GridView mGridView;
 
    private ImageAdapter mAdapter;



    @Override
 
    public void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.activity_main);



        mIsPlaying = false;



        ViewGroup.LayoutParams layoutParams;



        primesLE = 0;
 
        int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
 
        int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);



        View relativeLayout = findViewById(R.id.relativeLayout);
 
        View title_horizontalScrollView = relativeLayout.findViewById(R.id.title_horizontalScrollView);
 
        View dataLayout = title_horizontalScrollView.findViewById(R.id.dataLayout);
 
        mGridView = (GridView) dataLayout.findViewById(R.id.mGridView);
 
        layoutParams = mGridView.getLayoutParams();
 
        layoutParams.width = 150*numOfColumns; //this is in pixels
 
        mGridView.setLayoutParams(layoutParams);
 
        mGridView.setNumColumns(numOfColumns);
 
        mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
 
        mGridView.setAdapter(mAdapter);



        View inputLayout = relativeLayout.findViewById(R.id.inputLayout);
 
        EditText inputEditText = (EditText)inputLayout.findViewById(R.id.inputEditText);



        //list of what doesn't work



        /*
 
        inputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
 
            @Override
 
            public void onFocusChange(View v, boolean hasFocus) {
 
                if (hasFocus) {
 
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 
                }
 
            }
 
        });
 
        */
 
        /*
 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 
        imm.showSoftInput(inputEditText, InputMethodManager.SHOW_IMPLICIT);
 
        */



        /*
 
        inputEditText.setFocusableInTouchMode(true);
 
        inputEditText.setFocusable(true);
 
        InputMethodManager imm = (InputMethodManager) MainActivity.this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
 
        imm.hideSoftInputFromWindow(inputEditText.getWindowToken(), 0);
 
        inputEditText.requestFocus();
 
        imm.showSoftInput(inputEditText, 0);
 
        */
 
        /*        
 
            inputEditText.setOnKeyListener(new View.OnKeyListener() {

           @Override
 
            public boolean onKey(View v, int keyCode, KeyEvent event) {
 
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 
                imm.hideSoftInputFromWindow(
 
                    inputEditText.getWindowToken(), 0);
 
                return true;
 
            }
            });
 
         */   
    }

    @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;
     }

    private void playSoE(){

        for(int i = 0; i < primesLE; i++){
             mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i);
         }
     }

    @Override
 
    public boolean onOptionsItemSelected(MenuItem item) {
 
       // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();
         Log.d("Menu","Button Pressed");
 
        //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
 
            return true;
         }

        else if (id == R.id.action_status){
 
            if(mIsPlaying) {
 
                mIsPlaying = false;
 
                item.setIcon(R.drawable.ic_action_play);
 
                item.setTitle("Play");
 
                playSoE();
             }
             else {
                 mIsPlaying = true;
                 item.setIcon(R.drawable.ic_action_pause);
                 item.setTitle("Pause");
             }
            return true;
        }
        return super.onOptionsItemSelected(item);
 
    }
}
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
 
    android:layout_height="match_parent"
 
    android:id="@+id/relativeLayout"
    <LinearLayout
 
        android:layout_width="match_parent"
 
        android:layout_height="50dp"
 
        android:id="@+id/inputLayout"
 
        android:layout_alignParentTop="true"
 
        android:background="#a9a9a9"
 
        >
 
        <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:layout_gravity="center_vertical"
 
            android:id="@+id/textView"
 
            android:text="All primes lower this number:"
 
            />



        <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
 
        <LinearLayout
 
            android:focusable="true" android:focusableInTouchMode="true"
 
            android:layout_width="0px" android:layout_height="0px"/>



        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
 
             to prevent the dummy from receiving focus again -->
 
        <AutoCompleteTextView android:id="@+id/autotext"
 
            android:layout_width="fill_parent" android:layout_height="wrap_content"
 
            android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>



        <EditText
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:layout_gravity="center_vertical"
 
            android:inputType="numberSigned"
 
            android:ems="10"
 
            android:id="@+id/inputEditText"
 
            android:layout_weight="1" />
 
    </LinearLayout>




    <HorizontalScrollView
 
        android:layout_width="match_parent"
 
        android:layout_height="wrap_content"
 
        android:layout_below="@+id/inputLayout"
        android:id="@+id/title_horizontalScrollView"
 
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
 
            android:layout_height="match_parent"
 
            tools:context=".MainActivity"
 
            android:id="@+id/dataLayout"
            >

            <GridView
                android:id="@+id/mGridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:columnWidth="90dp"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:layout_alignParentBottom="true" />

        </RelativeLayout>

    </HorizontalScrollView>


</RelativeLayout>


ImageAdapter.java:

public class ImageAdapter extends ArrayAdapter {
     private Context mContext;
     private int mCount;
     private boolean setBlueBackground = false;
     private int[] gridColors;

    public ImageAdapter(Context c, int resource, int count) {
         super(c, resource);
         mContext = c;
         mCount = count;
         gridColors = new int[mCount];
         Arrays.fill(gridColors,Color.LTGRAY);
     }


    public int getCount() {
         return mCount;
     }
    public Object getItem(int position) {
         return null;
     }
    public long getItemId(int position) {
 
        return 0;
 
    }

    // create a new ImageView for each item referenced by the Adapter
 
    public View getView(int position, View convertView, ViewGroup parent) {
         TextView textView;
        if (convertView == null) {
 
            // if it's not recycled, initialize some attributes
 
            textView = new TextView(mContext);
 
            textView.setGravity(Gravity.CENTER);
 
            textView.setLayoutParams(new GridView.LayoutParams(100, 100));
        } else {
 
            textView = (TextView) convertView;
        }
        textView.setBackgroundColor(gridColors[position]);
       textView.setText("" + position);
       return textView;
 
    }

    public void setPositionColor(int position, int color) {
 
       gridColors[position] = color;
         notifyDataSetChanged();
 
    }
}
}


menu_main.xml:

<?xml version="1.0" encoding="utf-8"?>
 
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
 
        xmlns:app="http://schemas.android.com/apk/res-auto">
 
    <item android:id="@+id/action_status"
 
        android:icon="@drawable/ic_action_play"
 
        android:title="Play"
 
        app:showAsAction="always"/>
 
    <item android:id="@+id/action_stop"
 
        android:icon="@drawable/ic_action_stop"
 
        android:title="Stop"
 
        app:showAsAction="always"/>
 
    <item android:id="@+id/action_settings"
 
            android:title="@string/action_settings"
 
            app:showAsAction="never"  />



    </menu>


搞了好久,还是不能正确的弹出软键盘

问题定位

我觉得应该是 AutoCompleteTextView 覆盖了  inputEditText 的原因,你可以尝试调整一下位置.添加 added android:orientation="vertical"  ,参考如下代码

<LinearLayout
 
        android:layout_width="match_parent"
 
        android:layout_height="50dp"
 
        android:id="@+id/inputLayout"
 
        android:layout_alignParentTop="true"
 
        android:background="#a9a9a9"
 
        android:orientation="vertical">
 
        <TextView
 
            android:layout_width="wrap_content"
 
            android:layout_height="wrap_content"
 
            android:layout_gravity="center_vertical"
 
            android:id="@+id/textView"
 
            android:text="All primes lower this number:"
 
            />



        <EditText
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:inputType="numberSigned"
 
        android:ems="10"
 
        android:id="@+id/inputEditText"/>



        <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
 
        <LinearLayout
 
            android:focusable="true" android:focusableInTouchMode="true"
 
            android:layout_width="0px" android:layout_height="0px"/>



        <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
 
             to prevent the dummy from receiving focus again -->
 
        <AutoCompleteTextView android:id="@+id/autotext"
 
                              android:layout_width="fill_parent" android:layout_height="wrap_content"
 
                              android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>




    </LinearLayout>


 

处理方法


附带其他的解决方法:

方法一: 
在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 
例如

:<activity android:name=".Main" 
                  android:label="@string/app_name" 
                  android:windowSoftInputMode="adjustUnspecified|stateHidden" 
                  android:configChanges="orientation|keyboardHidden"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 

方法二: 
让EditText失去焦点,使用EditText的clearFocus方法 
例如:

EditText edit=(EditText)findViewById(R.id.edit); 
           edit.clearFocus(); 


方法三: 
强制隐藏Android输入法窗口 
例如:

EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0); 

2.EditText始终不弹出软件键盘 

例:EditText edit=(EditText)findViewById(R.id.edit); 
       edit.setInputType(InputType.TYPE_NULL); 


原文地址:http://www.itmmd.com/201505/725.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

  上一篇
下一篇  LinearLayout + TextView 树形菜单
精彩回复
我要追加问题,请求站长解决!
姓名: