Android EditText输入键监听器

时间:2015-02-18 20:29:29

标签: java android

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

<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:

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

1 个答案:

答案 0 :(得分:12)

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

<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"/>