Android EditText输入键监听器 [英] Android EditText enter key listener

查看:152
本文介绍了Android EditText输入键监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EditText字段,我想在Enter键上启动我的代码,但我仍然在EditText字段中只有新行。你能帮帮我吗?
这是我的源代码:
在XML布局中:

Please I have EditText field and I want on Enter key to start some my code, but I still got only new line in EditText field. Please can you help me? Here is my source code: In XML layout:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hledej_text"
        android:id="@+id/hledej"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="217dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textHledani"
        android:layout_above="@+id/hledej"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Napis co chces hledat"
        android:imeOptions="actionDone"
        android:inputType="text|textMultiLine"/>
</RelativeLayout>

和MainActivity.java:

And MainActivity.java:

public class MainActivity extends ActionBarActivity {

    private EditText textHledani;
    private Button hledej;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textHledani = (EditText)findViewById(R.id.textHledani);
        hledej = (Button)findViewById(R.id.hledej);
        hledej.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(textHledani.getText().toString().isEmpty()){
                    Toast.makeText(v.getContext(),"Nevim co mam hledat!",Toast.LENGTH_LONG).show();
                }
                else{
                    provedHledani();
                }
            }
        });
        textHledani.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))){
                    provedHledani();
                    return true;
                }
                else{
                    return false;
                }
            }
        });
    }

    private void provedHledani(){
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY,textHledani.getText().toString());
        startActivity(intent);
    }


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

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


推荐答案

尝试将布局中的EditText更改为以下代码:

Try change EditText in layout to code below:

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textHledani"
        android:layout_above="@+id/hledej"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Napis co chces hledat"
        android:imeOptions="actionDone"
        android:singleLine="true"
        android:lines="1"
        android:inputType="text"/>

这篇关于Android EditText输入键监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆