setOnEditorActionListener对我不起作用

克里斯

我只是在学习java / android,而setOnEditorActionListener却有问题。

我的问题是网页第一次仅通过单击“转到”按钮而不是键盘上的“完成”按钮加载。第一次之后,可以从键盘上的“完成”按钮加载网页。

onConfigurationChanged也存在问题,但我将作为一个单独的问题寻求帮助。

任何帮助或建议,将不胜感激。

我希望两个按钮都能一直工作。

activity_main.xml

 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >


<TableRow
android:id="@+id/tableRow1"
andoid:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
    android:id="@+id/UrlText"
    android:hint="Enter URL"
    android:layout_width="600px"
    android:layout_height="wrap_content"
    android:inputType="textUri"
    android:singleLine="true"
    android:lines="1"
    android:imeOptions="actionDone">

    <requestFocus />

</EditText>
<Button
    android:id="@+id/button1"
    android:onClick="onClick"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Go" />



</TableRow>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Main_Activity.java

package com.authorwjf.kbdtoggled;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;
import android.widget.Toast;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView.OnEditorActionListener;
import android.content.res.Configuration;


public class MainActivity extends Activity implements OnClickListener {


private WebView wv;
private InputMethodManager mIMEMgr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mIMEMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    findViewById(R.id.button1).setOnClickListener(this);
}

@Override
public void onClick(View v) {
 //
  // function to detect when the send button is pressed and if it is
  // get the text from the edittext box
  // then condition it to have http attributes
  // finally, close the keyboard
  //
    EditText editText = (EditText) findViewById(R.id.UrlText);
    editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            //
            // test to see if the send button was pressed
            //
          if (actionId == EditorInfo.IME_ACTION_DONE) {
            //
            //  toast ouput for debugging
            //
                Context context = getApplicationContext();
                CharSequence text = "Hello toast!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

                handled = true;
                //
                // hide the keyboard
                //
                mIMEMgr.hideSoftInputFromWindow(findViewById(R.id.UrlText).getWindowToken(), 0);
                //
                // now, get the id of the webview
                //
                wv = (WebView) findViewById(R.id.webView1);
                //
                // define the methods to apply to the webview
                //
                WebSettings settings = wv.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setBuiltInZoomControls(true);
                //
                // apply the methods to the webview client
                //
                wv.setWebViewClient(new MyWebViewClient());
                //
                // condition the url sting
                //
                EditText et = (EditText) findViewById(R.id.UrlText);
                String url = et.getText().toString().trim();
                //
                // and now load the webpage
                //
                wv.loadUrl("http://" + url);

            }
            return handled;
        }
    });


     if (v.getId() == R.id.button1)  {
        //
        // hide the keyboard
        //
        mIMEMgr.hideSoftInputFromWindow(findViewById(R.id.UrlText).getWindowToken(), 0);
        //
        // now, get the id of the webview
        //
        wv = (WebView) findViewById(R.id.webView1);
        //
        // define the methods to apply to the webview
        //
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        //
        // apply the methods to the webview client
        //
        wv.setWebViewClient(new MyWebViewClient());
        //
        // condition the url sting
        //
        EditText et = (EditText) findViewById(R.id.UrlText);
        String url = et.getText().toString().trim();
        //
        // and now load the webpage
        //
        wv.loadUrl("http://" + url);

         //  toast ouput for debugging

         Context context = getApplicationContext();
         CharSequence text = "Hello toast from onClick2";
         int duration1 = Toast.LENGTH_SHORT;

         Toast toast1 = Toast.makeText(context, text, duration1);
         toast1.show();
    }
  //
}

//
// class to listen for the keyboard send button
//




private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        return false;
    }

}
@Override
public void onConfigurationChanged(Configuration newConfig) {
 //   Configuration newConfig = getResources().getConfiguration();
    super.onConfigurationChanged(newConfig);
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.authorwjf.kbdtoggled"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:configChanges="orientation|keyboardHidden"
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="adjustResize|stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Farouk Touzi

尝试移动这部分代码:

EditText editText = (EditText) findViewById(R.id.UrlText);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        //
        // test to see if the send button was pressed
        //
      if (actionId == EditorInfo.IME_ACTION_DONE) {
        //
        //  toast ouput for debugging
        //
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            handled = true;
            //
            // hide the keyboard
            //
            mIMEMgr.hideSoftInputFromWindow(findViewById(R.id.UrlText).getWindowToken(), 0);
            //
            // now, get the id of the webview
            //
            wv = (WebView) findViewById(R.id.webView1);
            //
            // define the methods to apply to the webview
            //
            WebSettings settings = wv.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setBuiltInZoomControls(true);
            //
            // apply the methods to the webview client
            //
            wv.setWebViewClient(new MyWebViewClient());
            //
            // condition the url sting
            //
            EditText et = (EditText) findViewById(R.id.UrlText);
            String url = et.getText().toString().trim();
            //
            // and now load the webpage
            //
            wv.loadUrl("http://" + url);

        }
        return handled;
    }
});

超出onClick()方法。将其移到您的onCreate()方法中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章