phonegap: How to show and hide soft keyboard in Android
Create a class that handles the keyboard
import com.phonegap.DroidGap;
t.Context;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;滴的组词
public class KeyBoard {
private WebView mAppView;
private DroidGap mGap;
public KeyBoard(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void showKeyBoard() {
InputMethodManager mgr = (InputMethodManager)
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
((InputMethodManager)
}
public void hideKeyBoard() {
InputMethodManager mgr = (InputMethodManager)
mgr.WindowToken(), 0);
}
}
In your main activity add a Javascript interface for this class:
import android.os.Bundle;
import com.phonegap.*;
public class Mobile_PG extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
有山的诗句
KeyBoard keyboard = new KeyBoard(this, appView);
appView.addJavascriptInterface(keyboard, "KeyBoard");
super.loadUrl("file:///android_ast/www/index.html");
}
}
From the javascript you now can call window.KeyBoard.showKeyBoard(); to show the soft keyboard and if needed you can hide it with window.KeyBoard.hideKeyBoard();
I copy the l file into l and then ant compile and ant install. This is how l looks like: <?xml version="1.0" encoding="UTF-8"?>
<project name="Mobile_PG">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked in in Version
Control Systems. -->
<property file="local.properties" />
<!-- The build.properties file can be created by you and is never touched
by the 'android' tool. This is the place to change some of the default property values
ud by the Ant rules.
Here are some properties you may want to change/update:
application.package
the name of your application package as defined in the manifest. Ud by the
'uninstall' rule.
source.dir
the name of the source directory. Default is 'src'.
out.dir
the name of the output directory. Default is 'bin'.
Properties related to the SDK location or the project target should be updated
using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your application and
should be checked in in Version Control Systems.
-->
<property file="build.properties" />
<!-- The default.properties file is created and updated by the 'android' tool, as well
路由器设置登录as ADT.
This file is an integral part of the build system for your application and
should be checked in in Version Control Systems. -->
<property file="default.properties" />
<!-- Custom Android task to deal with the project target, and import the proper rules.
This requires ant 1.6.0 or above. -->
<path id="android.antlibs">
<pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
<pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
<pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
<pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
<pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
</path>
<taskdef name="tup"
classname="com.android.ant.SetupTask"
classpathref="android.antlibs"/>
<!-- Execute the Android Setup task that will tup some properties specific to the target,
and import the build rules files.
The rules file is imported from
<SDK>/platforms/<target_platform>/templates/l
To customize some build steps for your project:
- copy the content of the main node <project> from l
-
paste it in l below the <tup /> task.
- disable the import by changing the tup task below to <tup import="fal" /> This will ensure that the properties are tup correctly but that your customized build steps are ud.
-->
<tup import="fal" />
<!-- Custom tasks -->
<taskdef name="aaptexec"
classname="com.android.ant.AaptExecLoopTask"
classpathref="android.antlibs" />
<taskdef name="apkbuilder"
classname="com.android.ant.ApkBuilderTask"
classpathref="android.antlibs" />
<taskdef name="xpath"
classname="com.android.ant.XPathTask"
classpathref="android.antlibs" />
<!-- Properties -->
<!-- Tells adb which device to target. You can change this from the command line
by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
the emulator. -->
<property name="adb.device.arg" value="" />
<property name="ls.dir" location="${sdk.dir}/tools" />
<!-- Name of the application package extracted from manifest file -->
<xpath input="l" expression="/manifest/@package"
output="manifest.package" />
想念的诗词
<!-- Input directories -->
<property name="source.dir" value="src" />
<property name="source.absolute.dir" location="${source.dir}" />
<property name="gen.dir" value="gen" />
<property name="gen.absolute.dir" location="${gen.dir}" />
<property name="resource.dir" value="res" />
<property name="resource.absolute.dir" location="${resource.dir}" />
<property name="ast.dir" value="asts" />
<property name="ast.absolute.dir" location="${ast.dir}" />
<!-- Directory for the third party java libraries -->
<property name="external.libs.dir" value="libs" />
<property name="external.libs.absolute.dir" location="${external.libs.dir}" />
<!-- Directory for the native libraries -->
<property name="native.libs.dir" value="libs" />
<property name="native.libs.absolute.dir" location="${native.libs.dir}" />
<!-- Output directories -->
<property name="out.dir" value="bin" />
<property name="out.absolute.dir" location="${out.dir}" />
<property name="out.class.dir" value="${out.absolute.dir}/class" />
<property name="out.class.absolute.dir" location="${out.class.dir}" />
<!-- Intermediate files -->
<property name="dex.file.name" value="class.dex" />
<property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" /> <!-- The final package file to generate -->
<property name="out.debug.unaligned.package"
location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
<property name="out.debug.package"
location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
<property name="out.unsigned.package"
location="${out.absolute.dir}/${ant.project.name}-unsigned.apk" />
<property name="out.unaligned.package"
location="${out.absolute.dir}/${ant.project.name}-unaligned.apk" />
<property name="lea.package"
location="${out.absolute.dir}/${ant.project.name}-relea.apk" />
<!-- Verbosity -->
<property name="verbo" value="fal" />
<!-- This is needed by emma as it us multilevel verbosity instead of simple 'true' or 'fal' The property 'verbosity' is not ur configurable and depends exclusively on 'verbo'
value.-->
<condition property="verbosity" value="verbo" el="quiet">
<istrue value="${verbo}" />
</condition>
<!-- This is needed to switch verbosity of zipalign and aapt. Depends exclusively on 'verbo' -->
<condition property="v.option" value="-v" el="">
<istrue value="${verbo}" />
</condition>
<!-- This is needed to switch verbosity of dx. Depends exclusively on 'verbo' -->
<condition property="verbo.option" value="--verbo" el="">
<istrue value="${verbo}" />
</condition>
<!-- Tools -->
<condition property="exe" value=".exe" el=""><os family="windows" /></condition>
<property name="adb" location="${ls.dir}/adb${exe}" />
<property name="zipalign" location="${ls.dir}/zipalign${exe}" />
<!-- Emma configuration -->
<property name="emma.dir" value="${sdk.dir}/tools/lib" />
<path id="emma.lib">
<pathelement location="${emma.dir}/emma.jar" />
<pathelement location="${emma.dir}/emma_ant.jar" />
</path>
廉州往事
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
<!-- End of emma configuration -->
<!-- Macros -->
<!-- Configurable macro, which allows to pass as parameters output directory,
output dex filename and external libraries to dex (optional) -->
<macrodef name="dex-helper">
<element name="external-libs" optional="yes" />
<element name="extra-parameters" optional="yes" />
<quential>
<echo>Converting compiled files and external libraries into ${intermediate.dex.file}...
</echo>
</echo>
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate.dex.file}" />
白蚕豆<extra-parameters />
<arg line="${verbo.option}" />
<arg path="${out.class.absolute.dir}" />
<filet dir="${external.libs.absolute.dir}" includes="*.jar" />
<external-libs />
</apply>
</quential>
</macrodef>
<!-- This is macro that enable passing variable list of external jar files to ApkBuilder
Example of u:
<package-helper>
<extra-jars>
<jarfolder path="my_jars" />
<jarfile path="foo/bar.jar" />
捷杰耶夫<jarfolder path="your_jars" />
</extra-jars>
</package-helper> -->
合同<macrodef name="package-helper">
<attribute name="sign.package" />
<element name="extra-jars" optional="yes" />
<quential>
<apkbuilder
outfolder="${out.absolute.dir}"
baname="${ant.project.name}"
signed="@{sign.package}"
verbo="${verbo}">
<file path="${intermediate.dex.file}" />
<sourcefolder path="${source.absolute.dir}" />
<nativefolder path="${native.libs.absolute.dir}" />
<jarfolder path="${external.libs.absolute.dir}" />
<extra-jars/>
</apkbuilder>
</quential>
</macrodef>
<!-- This is macro which zipaligns in.package and outputs it to out.package. Ud by targets debug, -debug-with-emma and relea.-->
<macrodef name="zipalign-helper">
<attribute name="in.package" />
<attribute name="out.package" />
<quential>
<echo>Running zip align on </echo>
<exec executable="${zipalign}" failonerror="true">
<arg line="${v.option}" />
<arg value="-f" />
<arg value="4" />
<arg path="@{in.package}" />
<arg path="@{out.package}" />
</exec>
</quential>
</macrodef>
<!-- This is macro ud only for sharing code among two targets, -install and
-install-with-emma which do exactly the same but differ in dependencies -->
<macrodef name="install-helper">