简短的问题:是否可以(以及如何)从服务中显示软键盘?
长问题:我写了一个服务,它创建了一个"顶栏",显示在所有活动的顶部,包含一个EditText.我想在单击EditText时显示软键盘,但这不会发生.
当然我已经从服务的onFocusChange()和onClick()尝试了这个:
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Run Code Online (Sandbox Code Playgroud)
我想到的解决方法是通过扩展Activity类并添加AIDL接口来请求当前活动显示键盘.缺点是必须将每个键事件发送回服务(通过另一个AIDL接口)并手动转换为Unicode.
此外,如果当前活动包含EditText,则软键盘仅适用于活动,并且在选择服务的EditText时不再显示.
如果当前活动有EditText,会阻止服务的软键盘显示什么?这可能是Android的限制吗?
出于好奇,我尝试了这个代码,因为面试问题[*]
int main(int argc, char *argv[])
{
int a = 1234;
printf("Outer: %d\n", a);
{
int a(a);
printf("Inner: %d\n", a);
}
}
Run Code Online (Sandbox Code Playgroud)
在Linux上编译时(包括g ++ 4.6.3和clang ++ 3.0),它输出:
Outer: 1234
Inner: -1217375632
Run Code Online (Sandbox Code Playgroud)
但是在Windows(VS2010)上它打印:
Outer: 1234
Inner: 1234
Run Code Online (Sandbox Code Playgroud)
基本原理是,在第二个'a'变量的拷贝构造函数完成之前,第一个'a'变量仍然可以访问.但是我不确定这是标准行为,还是仅仅是(另一个)微软的怪癖.
任何的想法?
[*]实际问题是:
如何在不使用临时或全局变量的情况下使用包含范围中具有相同名称的变量的值初始化范围内的变量?
{
// Not at global scope here
int a = 1234;
{
int a;
// how do you set this a to the value of the containing scope a ?
}
}
Run Code Online (Sandbox Code Playgroud) 我想使用AIDL将String和Bitmap传递给服务.服务实现此AIDL方法:
void addButton(in Bundle data);
Run Code Online (Sandbox Code Playgroud)
在我的例子中,Bundle包含一个String和一个Bitmap.
调用应用程序(客户端)具有以下代码:
...
// Add text to the bundle
Bundle data = new Bundle();
String text = "Some text";
data.putString("BundleText", text);
// Add bitmap to the bundle
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.myIcon);
data.putParcelable("BundleIcon", icon);
try {
myService.addButton(data);
} catch (RemoteException e) {
Log.e(TAG, "Exception: ", e);
e.printStackTrace();
}
...
Run Code Online (Sandbox Code Playgroud)
在服务端,我有一个带有以下代码的ButtonComponent类:
public final class ButtonComponent implements Parcelable {
private final Bundle mData;
private ComponComponent(Parcel source) {
mData = source.readBundle();
}
public String getText() {
return mData.getString("BundleText"); …Run Code Online (Sandbox Code Playgroud) 我需要根据其ID更新PriorityQueue中的一些固定优先级元素.我认为这是一种非常常见的情况,这是一个示例代码段(Android 2.2):
for (Entry e : mEntries) {
if (e.getId().equals(someId)) {
e.setData(newData);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使Entry"不可变"(没有setter方法),以便创建一个新的Entry实例并由setData()返回.我将我的方法修改为:
for (Entry e : mEntries) {
if (e.getId().equals(someId)) {
Entry newEntry = e.setData(newData);
mEntries.remove(e);
mEntries.add(newEntry);
}
}
Run Code Online (Sandbox Code Playgroud)
代码似乎运行正常,但有人指出在迭代时修改队列是一个坏主意:它可能抛出一个ConcurrentModificationException,我需要将我想要删除的元素添加到ArrayList并稍后删除它.他没有解释原因,对我来说这看起来很费劲,但我在互联网上找不到任何具体的解释.
(这个帖子很相似,但优先级可以改变,这不是我的情况)
任何人都可以澄清我的代码有什么问题,我应该如何改变它 - 最重要的是 - 为什么?
谢谢,Rippel
PS:一些实施细节......
PriorityQueue<Entry> mEntries = new PriorityQueue<Entry>(1, Entry.EntryComparator());
Run Code Online (Sandbox Code Playgroud)
有:
public static class EntryComparator implements Comparator<Entry> {
public int compare(Entry my, Entry their) {
if (my.mPriority < their.mPriority) {
return 1;
}
else if (my.mPriority > their.mPriority) {
return -1; …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于UITableViews的主题没有在iPhone上刷新,但找不到符合我情况的任何内容,所以我正在寻求帮助.
在我的类中,它扩展了UIViewController,我有一个TableView和一个'ElementList'(它是NSMutableArray的包装器)用作数据源.
一个单独的线程通过'updateList:'方法向数组添加一个新元素.发生这种情况时,我希望自动刷新tableView,但这不会发生.
通过调试我的应用程序,我可以看到'cellForRowAtIndexPath'永远不会被调用,我无法找出原因.
我试图添加一个Observer,它调用'reloadTableView'方法(它实际上被调用),但tableView没有更新.
这是我的代码:
#import <UIKit/UIKit.h>
@interface ListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *tableView;
ElementList *elementList; // Wrapper for NSMutableArray
}
// Called by someone else, triggers the whole thing
-(void)updateList:(Element *)element;
// Added out of desperation
-(void)reloadTableView;
@end
@implementation ListViewController
-(void)loadView
{
// Create the TableView
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
assert(tableView != nil);
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
self.view = tableView;
// Added out of desperation
[[NSNotificationCenter defaultCenter] addObserver:self …Run Code Online (Sandbox Code Playgroud)