setOnEditorActionListener不适合我

时间:2014-12-22 14:54:20

标签: java android android-manifest

我刚刚学习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>

1 个答案:

答案 0 :(得分:1)

尝试移动这部分代码:

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

out of onClick()方法。将其移动到onCreate()方法中。