本文介绍了如何使EditText上重新获得焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个EditText和一个按钮一个活动。当pressed的按钮,我称之为

I have one activity with an EditText and a button. When the button is pressed, I call

myEditText.setClickable(false);
myEditText.setFocusable(false);

我还有一个按钮,当pressed,转变活动。

I have another button, which when pressed, changes the activity.

Intent myIntent = new Intent(view.getContext(), DestinationScreen.class);
startActivityForResult(myIntent, 0);

当我从活性2返回到它具有的EditText我的主要活动,我希望它恢复焦点。也就是说,我希望能够在键入它一些新的价值。任何想法,这是怎么可能呢?

When I return from activity2 to my main activity which has the EditText, I want it to regain the focus. That is, I want to be able to type in some new values in it. Any idea how that is possible?

我试图做到这一点,我在主体活动

I tried to do this in my main Activity

startActivityForResult(myIntent, 0);
myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();

这似乎并没有工作。

It doesn't seem to work.

推荐答案

正如你所说,你会喜欢的EditText 来重新获得焦点,当你从第二个活动返回。
那么很可能这就是你应该尝试:既然你已经调用与 startActivityForResult 办法(要求code:0)的活性2,你可以利用它:

As you said, you'd like the EditText to regain focus when you return from the second activity.
Then probably that's what you should try: since you are already invoking the activity2 with the startActivityForResult method (requestCode: 0), you could take advantage of it:

您应该重写

onActivityResult(int requestCode, int resultCode, Intent data)

方法的主要活动里面,检查是否请求code == 0 ,如果是这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case 0:
            EditText myEditText = (EditText) findViewById(R.id.textBox);
            myEditText.setClickable(true);
            myEditText.setFocusable(true);
            myEditText.requestFocus();
        default:
            break;
    }
}

这篇关于如何使EditText上重新获得焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:29