在调用显示ProgressDialog的新Activity后隐藏键盘

时间:2012-05-03 10:01:44

标签: android dialog android-activity progressdialog android-input-method

我在使用屏幕键盘时出现问题。我有一个显示键盘的EditText活动和一个转到第二个活动的按钮。第二个活动在其ProgressDialog上显示onCreate(),执行操作,并取消ProgressDialog。问题是,当显示ProgressDialog时,键盘也是如此。

我希望键盘在创建ProgressDialog之前消失。我搜索了StackOverflow和其他网站,但似乎没有任何东西可以用于这个特定场景。

我附上两张照片供你参考:

http://i45.tinypic.com/2rzq7b6.png http://i45.tinypic.com/34ret1z.png

这是第一项活动的代码:

public class FirstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

这是第二项活动的代码:

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        // TODO: hide keyboard here

        final ProgressDialog dialog = ProgressDialog.show(this, "", "Please wait...", true, false, null);

        // in real code, here there is an AsyncTask doing stuff...
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 5000);
    }
}

由于

5 个答案:

答案 0 :(得分:20)

使用phalt发布的技术变体解决:

InputMethodManager im = (InputMethodManager) this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

此代码在onCreate / onStart / onResume期间正常运行,因为不依赖于焦点视图来获取窗口令牌。

答案 1 :(得分:10)

将此代码写入manifest.xml文件中以获取“SecondActivity”活动。

<activity name="EditContactActivity"
    android:windowSoftInputMode="stateAlwaysHidden">
    ...
</activity>

答案 2 :(得分:1)

你也可以这样使用:

InputMethodManager imm;

在onCreate()方法中写下以下行:

imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

此行显示在按钮上:

imm.hideSoftInputFromWindow(arg0.getWindowToken(),0);

示例:

public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            imm.hideSoftInputFromWindow(arg0.getWindowToken(), 0);
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}
}

答案 3 :(得分:0)

如果在片段类

@Override
    public void onResume()
    {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onResume();
        }

如果在活动类

 @Override
    public void onResume()
    {
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onResume();
        FetchLocation fl = new FetchLocation(mContext, locationResult);
    }

答案 4 :(得分:-1)

你试过了吗?

InputMethodManager im = (InputMethodManager)
this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

im.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputManagerMethod.HIDE_NOT_ALWAYS);

这是我想要隐藏键盘的点的代码。