开发者

Android: Start activity when EditText is clicked

How do I start a new activity when the user touches an EditText like in the Facebook search and Google search widget?

Setting setOnClickListener works only after the first click. On the first click the Edit开发者_开发技巧Text becomes highlighted, and keyboard pops up. On second click it opens the new activity. I do not want this, instead I want to open the activity on the very first click. How do I do it?


You need to disable the EditText's focus in touch mode, that will make the onclick execute on the first tap:

<EditText ...
      android:focusableInTouchMode="false" 
      android:editable="false"
/>


Set the input type of the EditText to InputType.TYPE_NULL:

editText.setInputType(InputType.TYPE_NULL);

, which hides the soft keyboard while receiving user interaction. Start the activity:

public void onEditTextClick(View arg0) 
{
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
}

Of course, the onEditTextClick method has to be registered to the EditText object:)


Please, Try this

<EditText
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="search"/>

and

public void search(View view) 
{
    EditText text = (EditText)view;

    if(text.length() == 0)
        return;
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(intent);
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜