此处将为大家介绍关于用java写Unicode的详细内容,并且为您解答有关Sindhi的keyListener实现的问题的相关问题,此外,我们还将为您介绍关于android–HoneycombActi
此处将为大家介绍关于用java写Unicode的详细内容,并且为您解答有关Sindhi的keyListener实现的问题的相关问题,此外,我们还将为您介绍关于android – Honeycomb ActionBar中的SearchView上的KeyListener不起作用、android – 为每个ListView行而不是onItemClickListener实现onClickListener是否存在缺陷?、Android:为按钮覆盖onKeyListener的问题、Java KeyListener与Keybinding的有用信息。
本文目录一览:- 用java写Unicode(Sindhi)的keyListener实现的问题(java使用unicode)
- android – Honeycomb ActionBar中的SearchView上的KeyListener不起作用
- android – 为每个ListView行而不是onItemClickListener实现onClickListener是否存在缺陷?
- Android:为按钮覆盖onKeyListener的问题
- Java KeyListener与Keybinding
用java写Unicode(Sindhi)的keyListener实现的问题(java使用unicode)
我想以jTextField
这种方式通过keyListener的实现使用unicode :
textField.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent evt) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent evt) { // TODO Auto-generated method stub char var = evt.getKeyChar(); if(var == ''a''){ String values = urlTextField.getText() + Sindhi.ALIF; urlTextField.setText(values); } } });
但它写入English
字符a
与unicode
字符Sindhi.ALIF
。如何只获取unicode
手写的字符jTextField
答案1
小编典典无论您当前遇到什么问题,都不应在JTextField中使用KeyListener。请改用DocumentListener或DocumentFilter。给定您的代码,我猜测您需要的是DocumentFilter,因为您希望在输入JTextField并在显示之前更改JTextField的文本。
例如,
import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.DocumentFilter;import javax.swing.text.PlainDocument;public class SwapAForAleph { // No idea of the correct unicode for this!!! public static final char SINDHI_ALIF = ''\u0623''; public static void main(String[] args) { final JTextField textField = new JTextField(10); textField.setFont(textField.getFont().deriveFont(32f)); PlainDocument doc = (PlainDocument) textField.getDocument(); doc.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { text = filterText(text); super.insertString(fb, offset, text, attr); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { text = filterText(text); super.replace(fb, offset, length, text, attrs); } private String filterText(String text) { return text.replace(''a'', SINDHI_ALIF); } }); JPanel panel = new JPanel(); panel.add(textField); JOptionPane.showMessageDialog(null, panel); }}
或以另一种方式看…
import java.awt.ComponentOrientation;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.DocumentFilter;import javax.swing.text.PlainDocument;public class NonEnglishTextField { public static final char ALEPH = ''\u05D0''; public static void main(String[] args) { final JTextField textField = new JTextField(20); textField.setFont(textField.getFont().deriveFont(32f)); textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); textField.setHorizontalAlignment(SwingConstants.RIGHT); PlainDocument doc = (PlainDocument) textField.getDocument(); doc.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { text = filterText(text); super.insertString(fb, offset, text, attr); } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { text = filterText(text); super.replace(fb, offset, length, text, attrs); } private String filterText(String text) { StringBuilder sb = new StringBuilder(); for (char c : text.toLowerCase().toCharArray()) { if (c >= ''a'' && c <= ''z'') { char newChar = (char) (c - ''a'' + ALEPH); sb.append(newChar); } else { sb.append(c); } } return sb.toString(); } }); JPanel panel = new JPanel(); panel.add(textField); JOptionPane.showMessageDialog(null, panel); }}
android – Honeycomb ActionBar中的SearchView上的KeyListener不起作用
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.locations_map_menu,menu); ActionBar ab = getActionBar(); ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar)); ab.setdisplayShowTitleEnabled(false); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setonKeyListener(new OnKeyListener() { @Override public boolean onKey(View v,int keyCode,KeyEvent event) { return true; // This code never fires } }); return true; }
解决方法
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextChange(String newText) { // Do something return true; } @Override public boolean onQueryTextSubmit(String query) { // Do something return true; } }; searchView.setonQueryTextListener(queryTextListener);
android – 为每个ListView行而不是onItemClickListener实现onClickListener是否存在缺陷?
为ListView中的每一行注册一个唯一的OnClickListener会更方便,但我想确保这是一种可接受的做法.我的current design是一种相当复杂的方法,可以将OnClickListener的关注点与每个行类型分开.
原因是我在ListView中有多个行类.每个班级都有完全不同的责任和行为.例如,考虑一个可以包含子类别和书名的ListView.如果单击书名,则应开始显示封面图像的新活动.如果单击子类别,则会显示新的书籍和类别列表.
我希望该行本身能够保持对其自身身份和责任的了解,而不必泄漏有关onItemClickListener的实现者要维护的每一行的知识.
我还想知道这样做的性能影响与实现我自己的逻辑以确定如何处理点击有关.
为每个ListView ArrayAdapter行而不是onItemClickListener实现onClickListener是否有缺点?我正在寻找具体的数据和具体的缺点,而不是模糊的建议.
我是否应该期待内存使用,初始化时间或稳态速度(如滚动列表)受到显着影响?
解决方法:
您没有解释为什么每行需要单独的单击侦听器,但我建议不要这样做.看一下使用View.setTag(Object)/View.getTag()传递特定于行的自定义数据,通过它可以自定义(共享)单击侦听器的响应.
编辑
我从您的示例中看到为什么您希望将不同的OnClickListener附加到您的行.我的印象是你想为每一行单独的OnClickListener实例. (这是我推荐反对这一点的主要原因.)如果你有两种类型的行(类别和标题)和数百行,你只需要两种响应,而不是数百种.我也理解关于分离问题的观点.
不过,我认为重写ListActivity.onListItemClick()(或者如果不使用ListActivity则调用ListView.setonItemClickListener())会更干净,并且不太可能干扰列表视图的操作.您可以使用委托模式,如下所示.
定义抽象类或接口:
public interface MyClickHandler {
public void onItemClick(ListView l, View v, int position, long id);
}
然后为每种类型的行数据创建一个实现此接口的对象实例(每行不是一个实例).在适配器中,使用setTag(Object)将每行的标记初始化为相应的MyClickHander实例.在ListActivity.onListItemClick()重写中,使用以下逻辑:
protected void onListItemClick(ListView l, View v, int position, long id) {
Object tag = v.getTag();
if (tag instanceof MyClickHandler) {
((MyClickHandler) tag).onItemClick(l, v, position, id);
} else {
// default processing (if any)
}
}
Android:为按钮覆盖onKeyListener的问题
在按钮上按Enter键时,我需要某种功能.
当我重写onKey()时,我编写了要为KEY_ENTER执行的代码.这很好.
setupButton.setonKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_ENTER == keyCode)
{
Intent setupIntent = new Intent(getApplicationContext(),SetUp.class);
startActivityForResult(setupIntent, RESULT_OK);
}
return true;
}
});
但是现在没有其他按键可以像“向上”,“向下”箭头之类的按钮一样工作了.我知道这是因为我只为Enter覆盖了onKey().
但是,有没有办法我可以保留所有其他键的功能并仅对某些特定键覆盖?
注意:这是因为按下Enter键时不会调用我的onClick().因此,我需要重写onKey()本身.
—
琪琪
解决方法:
当您在函数中返回true时,这表明Android正在处理所有键,而不仅仅是Enter键.
您应该仅在if语句的结尾处返回true,并在函数末尾返回false.
setupButton.setonKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_ENTER == keyCode)
{
Intent setupIntent = new Intent(getApplicationContext(),SetUp.class);
startActivityForResult(setupIntent, RESULT_OK);
return true;
}
return false;
}
});
Java KeyListener与Keybinding
解决方法
KeyListener遇到与可聚焦性相关的问题以及GUI中的其他控件,焦点将始终远离组件(使用KeyListener).
一个简单的解决方案是使用Action
s API.这允许您定义一个自包含的“动作”,它充当ActionListener,但也带有可用于配置其他UI组件的配置信息,特别是按钮
例如…
采用可以表示任意数字的通用NumberAction(现在将其限制为0-9)……
public class NumberAction extends AbstractAction { private int number; public NumberAction(int number) { putValue(NAME,String.valueOf(number)); } public int getNumber() { return number; } @Override public void actionPerformed(ActionEvent e) { int value = getNumber(); // Do something with the number... } }
你可以做点什么……
// Create the action... NumberAction number1Action = new NumberAction(1); // Create the button for number 1... JButton number1Button = new JButton(number1Action); InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); // Create a key mapping for number 1... im.put(Keystroke.getKeystroke(KeyEvent.VK_1,0),"number1"); im.put(Keystroke.getKeystroke(KeyEvent.VK_NUMPAD1,"number1"); ActionMap am = getActionMap(); // Make the input key to the action... am.put("number1",number1Action);
而且你已经完成了……
您还可以为相同的数字创建任意数量的NumberAction实例,这意味着您可以单独配置UI和绑定,但是知道在触发时,它们将执行相同的代码逻辑,例如……
关于用java写Unicode和Sindhi的keyListener实现的问题的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于android – Honeycomb ActionBar中的SearchView上的KeyListener不起作用、android – 为每个ListView行而不是onItemClickListener实现onClickListener是否存在缺陷?、Android:为按钮覆盖onKeyListener的问题、Java KeyListener与Keybinding等相关内容,可以在本站寻找。
本文标签: