Android调用输入法软键盘,返回输入的内容
我们一直在讲,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;
- @Override
- protected 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() {
- @Override
- public 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);
- }
- });
- }
- @Override
- protected 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;
- }
- }
- @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;
- }
- static{
- System.loadLibrary("cocos2dinput");
- }
- }
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;
@Override
protected 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() {
@Override
public 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);
}
});
}
@Override
protected 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;
}
}
@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;
}
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;
- @Override
- protected 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);
- }
- @Override
- public 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;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- //inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
- //隐藏
- inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
- //传送内容到上一个Activity
- Bundle resultBundle=new Bundle();
- resultBundle.putString("contentString", editText.getText().toString().trim());
- mIntent.putExtra("resultBundle", resultBundle);
- setResult(RESULT_OK,mIntent);
- this.finish();
- }
- }
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;
@Override
protected 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);
}
@Override
public 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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//隐藏
inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//传送内容到上一个Activity
Bundle 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,完工。