gpt4 book ai didi

java - setOnEditorActionListener 不适合我

转载 作者:行者123 更新时间:2023-12-01 12:05:53 25 4
gpt4 key购买 nike

我刚刚学习 java/android,我在 setOnEditorActionListener 方面遇到问题。

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

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>

最佳答案

尝试移动这部分代码:

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() 方法中。

关于java - setOnEditorActionListener 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27605084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com