Android调用输入法软键盘,返回输入的内容

2024-06-17 11:08

本文主要是介绍Android调用输入法软键盘,返回输入的内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android调用输入法软键盘,返回输入的内容       

        分类:            Android 32人阅读 评论(0) 收藏 举报
Android NDK Android调出软键盘 InputMethodManager

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


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


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

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

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



工程名:Cocos2dInput

包名:com.example.cocos2dinput


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


MainActivity:

[cpp] view plain copy print ?
  1. package com.example.cocos2dinput; 
  2.  
  3. import android.hardware.input.InputManager; 
  4. import android.os.Bundle; 
  5. import android.R.integer; 
  6. import android.app.Activity; 
  7. import android.app.AlertDialog; 
  8. import android.app.AlertDialog.Builder; 
  9. import android.content.Context; 
  10. import android.content.DialogInterface; 
  11. import android.content.Intent; 
  12. import android.util.Log; 
  13. import android.view.Menu; 
  14. import android.view.View; 
  15. import android.view.View.OnClickListener; 
  16. import android.view.inputmethod.InputMethodManager; 
  17. import android.widget.Button; 
  18. import android.widget.TextView; 
  19.  
  20. public class MainActivity extends Activity { 
  21.      
  22.     TextView ContentTextView; 
  23.     Button button; 
  24.     Button buttonCallC; 
  25.     String contentString; 
  26.     public static Context mContext; 
  27.  
  28.     @Override 
  29.     protected void onCreate(Bundle savedInstanceState) { 
  30.         super.onCreate(savedInstanceState); 
  31.         setContentView(R.layout.activity_main); 
  32.          
  33.          
  34.         contentString=getStringFromC(); 
  35.         ContentTextView=(TextView)findViewById(R.id.text1); 
  36.         ContentTextView.setText(contentString); 
  37.         button=(Button)findViewById(R.id.button1); 
  38.         buttonCallC=(Button)findViewById(R.id.button2); 
  39.          
  40.         mContext=this.getApplicationContext(); 
  41.          
  42.         button.setOnClickListener(new OnClickListener() { 
  43.              
  44.             @Override 
  45.             public void onClick(View arg0) { 
  46.                 // TODO Auto-generated method stub 
  47.                 //InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
  48.                 //inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
  49.                  
  50.                 Intent intent=new Intent(); 
  51.                 intent.setClassName(getApplication(),"com.example.cocos2dinput.InputActivity"); 
  52.                 startActivityForResult(intent, 0); 
  53.             } 
  54.         }); 
  55.          
  56.     } 
  57.      
  58.      
  59.      
  60.     @Override 
  61.     protected void onActivityResult(int requestCode,int resultCode,Intent intent) 
  62.     { 
  63.         switch (resultCode) { 
  64.         case RESULT_OK: 
  65.             Bundle bundle=intent.getBundleExtra("resultBundle"); 
  66.             contentString=bundle.getString("contentString"); 
  67.             ContentTextView.setText(contentString); 
  68.             break
  69.  
  70.         default
  71.             break
  72.         } 
  73.     } 
  74.  
  75.     @Override 
  76.     public boolean onCreateOptionsMenu(Menu menu) { 
  77.         // Inflate the menu; this adds items to the action bar if it is present. 
  78.         getMenuInflater().inflate(R.menu.main, menu); 
  79.         return true
  80.     } 
  81.      
  82.     static
  83.         System.loadLibrary("cocos2dinput"); 
  84.     } 
  85.  
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:一个输入框和一个完成按钮

[java] view plain copy print ?
  1. package com.example.cocos2dinput; 
  2.  
  3. import android.os.Bundle; 
  4. import android.app.Activity; 
  5. import android.content.Context; 
  6. import android.content.Intent; 
  7. import android.view.Menu; 
  8. import android.view.View; 
  9. import android.view.View.OnClickListener; 
  10. import android.view.View.OnFocusChangeListener; 
  11. import android.view.inputmethod.InputMethodManager; 
  12. import android.widget.Button; 
  13. import android.widget.EditText; 
  14.  
  15. public class InputActivity extends Activity implements OnClickListener { 
  16.  
  17.     public EditText editText; 
  18.     public Button button; 
  19.     public Intent mIntent; 
  20.      
  21.     @Override 
  22.     protected void onCreate(Bundle savedInstanceState) { 
  23.         super.onCreate(savedInstanceState); 
  24.         setContentView(R.layout.activity_input); 
  25.          
  26.         editText=(EditText)findViewById(R.id.editText1); 
  27.         button=(Button)findViewById(R.id.button1); 
  28.          
  29.         mIntent=this.getIntent(); 
  30.          
  31.         button.setOnClickListener(this); 
  32.          
  33.          
  34.     } 
  35.  
  36.     @Override 
  37.     public boolean onCreateOptionsMenu(Menu menu) { 
  38.         // Inflate the menu; this adds items to the action bar if it is present. 
  39.         getMenuInflater().inflate(R.menu.input, menu); 
  40.         return true
  41.     } 
  42.  
  43.     @Override 
  44.     public void onClick(View v) { 
  45.         // TODO Auto-generated method stub 
  46.  
  47.         InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
  48.         //inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
  49.          
  50.         //隐藏 
  51.         inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
  52.          
  53.         //传送内容到上一个Activity 
  54.         Bundle resultBundle=new Bundle(); 
  55.         resultBundle.putString("contentString", editText.getText().toString().trim()); 
  56.         mIntent.putExtra("resultBundle", resultBundle); 
  57.         setResult(RESULT_OK,mIntent); 
  58.         this.finish();       
  59.     } 
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,完工。

这篇关于Android调用输入法软键盘,返回输入的内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1069272

相关文章

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

使用Python实现获取网页指定内容

《使用Python实现获取网页指定内容》在当今互联网时代,网页数据抓取是一项非常重要的技能,本文将带你从零开始学习如何使用Python获取网页中的指定内容,希望对大家有所帮助... 目录引言1. 网页抓取的基本概念2. python中的网页抓取库3. 安装必要的库4. 发送HTTP请求并获取网页内容5. 解

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

SpringCloud之LoadBalancer负载均衡服务调用过程

《SpringCloud之LoadBalancer负载均衡服务调用过程》:本文主要介绍SpringCloud之LoadBalancer负载均衡服务调用过程,具有很好的参考价值,希望对大家有所帮助,... 目录前言一、LoadBalancer是什么?二、使用步骤1、启动consul2、客户端加入依赖3、以服务

Python实现常用文本内容提取

《Python实现常用文本内容提取》在日常工作和学习中,我们经常需要从PDF、Word文档中提取文本,本文将介绍如何使用Python编写一个文本内容提取工具,有需要的小伙伴可以参考下... 目录一、引言二、文本内容提取的原理三、文本内容提取的设计四、文本内容提取的实现五、完整代码示例一、引言在日常工作和学

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音