为什么我的editText.requestFocus();工作?

时间:2016-03-11 10:20:02

标签: android android-edittext android-softkeyboard

当我的应用程序启动时,软键盘默认加载。我这样做

final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();

在我的onCreate方法中。在我的XML中,我的editText有android:inputType="phone",这就是键盘出现的原因。

当用户滚动我的列表视图时,键盘会消失,我也会在onCreate中消失:

if (scrollState !=0){
    InputMethodManager inputMethodManager = (InputMethodManager)
            getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
}

现在我已经在我的应用中添加了一个图像按钮。当用户点击它时,我希望键盘再次启动,但它无法正常工作。只是想知道为什么不,因为它似乎是一个非常基本的指令:

public void softkeyboardButton(View v)
{

    final EditText editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    Log.e("button", "Yep, it worked.");

}

这是我的整个代码:

package com.example.chris.sunil_gupta;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;

public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {

//create a ListView object called listview
        final ListView listview;


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.ListView_2);

        String[] countries =  {"Ireland", "France","England","Ireland", "France","England","Ireland", "France","England","Ireland", "France","England"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, countries);

        listview.setAdapter(adapter);

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();


listview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {



        Log.e("scrollState", "Yep, it worked.");


        if (scrollState !=0){
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0);
        }


    }
//    this method looks for changes in the edittext box.
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
});



        editText.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
//          If there are changes typed in the edittext, then change the height of the edittext from 0,
//            as described in the activity_main xml file
            public void afterTextChanged(Editable s) {

                float density=getResources().getDisplayMetrics().density;

                if (editText.length() > 0) {


//                    not sure what density and layoutparams do. I think we're
//                    converting pixels to dp
                    editText.getLayoutParams().height =(int)(50*density);
//                    editText.requestLayout();
                    Log.e("scrollState", "Yep, it worked.");

                }
//                if there is nothing in edittext, make it invisible
                else if (editText.length() == 0){

                    editText.getLayoutParams().height =(int)(0*density);

                }
                editText.requestLayout();

            }
        });





    }

    public void softkeyboardButton(View v)
    {

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();
        Log.e("button", "Yep, it worked.");

    }

    //This clears the edittext next time user starts the application, rather than
//    having the same numbers there, which the user probably doesn't want anymore
    protected void onResume() {
        final EditText editText = (EditText) findViewById(R.id.editText);
        super.onResume();
        editText.setText("");


    }

    }

1 个答案:

答案 0 :(得分:1)

试试这个

用于显示键盘

 InputMethodManager imm =   (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE); 
 imm.showSoftInput(ed, 0); 
 ed.requestFocus();

希望这有助于你