在我的应用程序中我使用editext(我称之为editext ---> url) 我会说“完成”键有动作我的按钮 我已经阅读了其他问用户,但没有理解如何制作
请问我的英语
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
url = (EditText) findViewById(R.id.url);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
String a= url.toString();
a = webView.getUrl();
}
});
webView.loadUrl("http://www.google.it");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private WebView webView;
private EditText url;
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:orientation="vertical" >
<ImageButton
android:id="@+id/reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/back"
android:layout_toLeftOf="@+id/back"
android:background="@drawable/refresh"
android:contentDescription="@string/reload"
android:src="@drawable/refresh"
android:text="@string/reload" />
<EditText
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="4dip"
android:layout_toLeftOf="@+id/back"
android:ems="10"
android:inputType="textUri"
android:longClickable="false"
android:text="@string/default_url"
android:textSize="@dimen/activity_horizontal_margin"
android:typeface="sans" />
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/forward"
android:background="@drawable/Back"
android:contentDescription="@string/back"
android:maxHeight="@dimen/activity_vertical_margin"
android:maxWidth="@dimen/activity_horizontal_margin"
android:src="@drawable/Back" />
<ImageButton
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/go"
android:background="@drawable/Forward"
android:contentDescription="@string/forward"
android:maxWidth="@dimen/activity_horizontal_margin"
android:src="@drawable/Forward" />
<ImageButton
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="2dip"
android:background="@drawable/Search"
android:contentDescription="@string/go"
android:maxHeight="@dimen/activity_horizontal_margin"
android:maxWidth="@dimen/activity_horizontal_margin"
android:onClick="go"
android:src="@drawable/Search"
android:text="@string/go" />
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/url" />
答案 0 :(得分:0)
如果我正确理解了您的问题,您希望键盘上的“完成”按钮执行某些操作。这应该可以解决问题:
url.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
// Your action goes here...
}
return false;
}
});