按回车键加载网址(android)

时间:2016-01-31 20:25:42

标签: java android android-webview

我正在制作一个Android浏览器,但当我点击Enter时,它会在edittext框中添加一个新行,而不是加载网址。请帮助我。来源是here

2 个答案:

答案 0 :(得分:1)

您可以使用Key事件。在edittext上设置setOnKeyListener并执行以下操作:

  if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) 
     //Load url from EditText

答案 1 :(得分:1)

尝试实施OnEditorActionListener界面。例如:

// Reference the edit text view.
EditText theEditText = (EditText) findViewById(R.id.theEditText);

// Set editor action listener.
theEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_NULL) { // Enter key was pressed.
            // Open link from url.
        }

        // Return true to prevent the edit text from adding a newline from the enter press.
        return false; // Allow the event to propagate, Return true to consume the event.
    }
});

如果按下回车键,actionId将等于EditorInfo.IME_NULL

希望这有帮助。