我刚刚启动并运行了 AVD,并让“HelloWorld”开始工作。 现在我认为下一个合乎逻辑的步骤是更熟悉 I/O,所以,我想创建和输入框,并有一个按钮(或某种触发器)在用户完成输入时点击,然后读取它,并根据输入输出响应。
我尝试使用 Android 开发者资源,它说要创建和输入方法,我需要编辑 AndroidManifest.xml 并将服务添加到其中。所以,我的 xml 现在看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hunter.nance.escapetheroom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidEscapeTheRoomActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="FastInputIME"
android:label="@string/fast_input_label"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method" />
</service>
</application>
但是,我的 Eclipse 文件显示错误:
[2012-02-27 11:44:41 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Unable to read C:\Documents and Settings\java\workspace\AndroidEscapeTheRoom\AndroidManifest.xml: org.eclipse.core.internal.resources.ResourceException: Resource is out of sync with the file system: '/AndroidEscapeTheRoom/AndroidManifest.xml'.
[2012-02-27 11:44:41 - AndroidEscapeTheRoom] AndroidManifest.xml does not declare a Java package: Build aborted.
更不用说,即使在让 XML 工作之后,我也不确定如何在我的实际 Java 代码中使用它。对此有什么好的教程或建议吗?
非常感谢!
最佳答案
您不需要任何权限来放置 textInput 和操作它的数据。打开布局文件夹,然后打开 Main xml 文件并转到设计 View 。然后您可以拖放文本输入。关于 Java 代码,您转到 src 文件夹,然后在您的包中您会找到一个 java 文件。要获得对文本输入的引用,您需要这样的东西。
EditText et = (EditText) findViewById(R.id.mytextinput); //considering your input id is mytextinput.
然后您可以使用两种方法获取和设置它的文本:
String myText = et.getText().toString();
et.setText("my new text for input box");
试着看这个:http://www.youtube.com/watch?v=0sS-ylTxi40
更新:
TextView tv = (TextView) findViewById(R.id.textView1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
tv.setText(et.getText().toString());
}
});
此代码将完全按照您的要求执行。只是不要忘记将具有相同 id-s 的控件放在 xml 中:)
关于Android开发--输入法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485578/