我们一直在讲,C++写游戏代码,然后通过NDK编译,然后展现在Android层。


在游戏中的各种输入框,点击就会出现Android的软键盘。这其实是C++调用了Android层,然后在Android层获得软键盘的输入内容,然后把内容返回到C++层。


我们这里讲的是Android层调出软键盘然后获取到用户输入显示在TextView上面,至于怎么将Android层获取到的内容返回到C++层,请看

C++调用Android 与Android调用C++ 例子

好了,下面是Android调用输入法软键盘获取用户输入的例子。



工程名:Cocos2dInput

包名:com.example.cocos2dinput


我们工程里面有两个Activity,一个是主界面,另外一个界面上有一个输入框加上一个完成按钮,点击完成按钮就把获取到的软键盘输入传递到主界面Activity


MainActivity:

package com.example.cocos2dinput;import android.hardware.input.InputManager;import android.os.Bundle;import android.R.integer;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {TextView ContentTextView;Button button;Button buttonCallC;String contentString;public static Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);contentString=getStringFromC();ContentTextView=(TextView)findViewById(R.id.text1);ContentTextView.setText(contentString);button=(Button)findViewById(R.id.button1);buttonCallC=(Button)findViewById(R.id.button2);mContext=this.getApplicationContext();button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);//inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);Intent intent=new Intent();intent.setClassName(getApplication(),"com.example.cocos2dinput.InputActivity");startActivityForResult(intent, 0);}});}@Overrideprotected void onActivityResult(int requestCode,int resultCode,Intent intent){switch (resultCode) {case RESULT_OK:Bundle bundle=intent.getBundleExtra("resultBundle");contentString=bundle.getString("contentString");ContentTextView.setText(contentString);break;default:break;}}@Overridepublic 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;}static{System.loadLibrary("cocos2dinput");}}


InputActivity:一个输入框和一个完成按钮

package com.example.cocos2dinput;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnFocusChangeListener;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.EditText;public class InputActivity extends Activity implements OnClickListener {public EditText editText;public Button button;public Intent mIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_input);editText=(EditText)findViewById(R.id.editText1);button=(Button)findViewById(R.id.button1);mIntent=this.getIntent();button.setOnClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.input, menu);return true;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubInputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);//inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//隐藏inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);//传送内容到上一个ActivityBundle resultBundle=new Bundle();resultBundle.putString("contentString", editText.getText().toString().trim());mIntent.putExtra("resultBundle", resultBundle);setResult(RESULT_OK,mIntent);this.finish();}}


1. 点击游戏中的输入框,触发事件

2. 从C++调用Android层的InputActivity,显示出Android层输入框和软键盘。

3. InputActivity中的输入框显示出了输入的内容,点击完成按钮,Android调用C++ 传递输入内容到上一个Activity(即游戏中)

4. 在C++层设置输入框显示。


OK,完工。

更多相关文章

  1. Android软键盘回车键修改为搜索按键
  2. 诡异特殊的EditText 弹出软键盘遮挡BUG
  3. Android TextView内容过长加省略号
  4. Android 自定义圆角按钮
  5. 2010.12.22——— android 一个永远处于底部的按钮集合
  6. Android EditText进入页面不聚焦设置(进入页面不弹出键盘,点击弹出

随机推荐

  1. linux下部署android模拟器
  2. Android(安卓)EditText不显示光标的解决
  3. android杀进程方法
  4. android:installLocation简析
  5. Android(安卓)build.gradle文件详解(转述
  6. android:screenOrientation横屏竖屏设置
  7. Android常用知识笔记
  8. Android(安卓)10ms问题:关于Android音频路
  9. Android(安卓)之 EditText属性用法介绍
  10. android 设置Button或者ImageButton的背