我创建了一个 CustomTextField 当我键入超出 TextField 宽度的文本时,它会向左滚动 使用 HorizonalFieldManager 的 但现在的问题是,如果我用鼠标右键单击并滚动它 它持续到长度不足 但不会停止到我输入的最后一个词 这里有什么问题 ?? 是不是bug
我只需要在它到达最后一个词时禁用 HorizontalScrolling 它应该能够在单词中最后一个单词的开头和结尾之间滚动
查看代码
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class CustomTextField extends VerticalFieldManager {
private int textWidth=0;
private int textHeight=0;
private BasicEditField basicEditField;
private HorizontalFieldManager hfm;
//Border border;
public CustomTextField(int width,int height) {
super();
textWidth=width;
textHeight=height;
//border=BorderFactory.createSimpleBorder(new XYEdges(1, 1, 1, 1));
hfm=new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
setExtent(textWidth, textHeight);
}
};
basicEditField=new BasicEditField("","",200,BasicEditField.NO_NEWLINE);
//basicEditField.setBorder(border);
hfm.add(basicEditField);
add(hfm);
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(textWidth, textHeight);
setExtent(textWidth, textHeight);
}
protected void paint(Graphics graphics) {
super.paint(graphics);
graphics.setColor(Color.BLACK);
graphics.drawRect(0,0, textWidth, textHeight);
}
}
我已经将它初始化为
CustomTextField textField=new CustomTextField(200, 20);
add(textField);
我觉得 HorizontalFieldManager 需要 Scroll(是滚动功能)...但是还没有想出解决方案 请帮忙
最佳答案
因此,在 BlackBerry 字段中,extent 是字段的实际视觉 大小。但是,虚拟范围 是它可以使用的逻辑 大小,其中一些可能不可见。对于要滚动的 Managers,通常会将虚拟范围设置为大于范围。
我使用这个概念来动态更改 HorizontalFieldManager 的虚拟范围,基于当前需要多少空间来勉强容纳 BasicEditField 中的文本。为此,我必须通过实现 FieldChangeListener 让 HorizontalFieldManager 监听对 BasicEditField 的更改。然后,当每个字符被输入到编辑字段时,水平字段管理器将重新计算字段中现在的文本量需要多少宽度。然后它将虚拟宽度重新设置为该宽度。
这导致水平字段管理器只允许滚动到输入文本的末尾,而不是向右滚动,这是代码最初的工作方式。
因此,我不认为黑莓手机做错了什么……操作系统中没有错误。以前,只是没有设置虚拟范围。
我将您的 HorizontalFieldManager 拆分为一个新的私有(private)类,因为我不喜欢在逻辑超过大约 5 行代码时使用匿名类。因此,下面的解决方案看起来有点不同。
其他想法:
1) 由于您尝试使用自定义 paint() 实现绘制边框,因此存在绘图伪像。但是,那个错误本来就在那里,我将这个问题解释为关于滚动问题。看起来您正在尝试使用 Border 对象,这可能是为滚动字段实现边框的更好方法。
2) 在我的新解决方案中,实际的 CustomTextField 类并没有太多内容。它只是 CustomHorizontalFieldManager 的容器 (Manager)。如果你愿意,你可以去掉那个外层。但是,我知道有时当您发布代码时,您会删除对您遇到问题的事情不重要的细节。因此,可能需要一个 VerticalFieldManager 包含一个 HorizontalFieldManager,其中包含一个 BasicEditField。我会把它留给你......不过它只是可选的清理。
3) 我在 5.0 Storm2 模拟器上对此进行了测试。
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.Arrays;
public class CustomTextField extends VerticalFieldManager {
private int textWidth = 0;
private int textHeight = 0;
private CustomHorizontalFieldManager hfm;
public CustomTextField(int width, int height) {
super();
textWidth = width;
textHeight = height;
hfm = new CustomHorizontalFieldManager();
add(hfm);
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(textWidth, textHeight);
setExtent(textWidth, textHeight);
}
protected void paint(Graphics graphics) {
// TODO: change me!
super.paint(graphics);
graphics.setColor(Color.BLACK);
graphics.drawRect(0, 0, textWidth, textHeight);
}
private class CustomHorizontalFieldManager extends HorizontalFieldManager implements FieldChangeListener {
private BasicEditField basicEditField;
/** the maximum virtual width of the edit field, based on the max num of chars */
private int maxVirtualWidth;
public CustomHorizontalFieldManager() {
super(Manager.HORIZONTAL_SCROLL);
int maxNumChars = 200;
basicEditField = new BasicEditField("", "", maxNumChars, BasicEditField.NO_NEWLINE);
// determine how wide the field would need to be to hold 'maxNumChars', with the font
// in use ... just pick a long string of all W's, since that's usually the widest char
char[] buffer = new char[maxNumChars];
Arrays.fill(buffer, 'W');
String spacer = new String(buffer);
maxVirtualWidth = basicEditField.getFont().getAdvance(spacer);
// we need to listen as the user types in this field, so we can dynamically alter its
// virtual width
basicEditField.setChangeListener(this);
add(basicEditField);
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
// extent is the visible size, virtual extent can be wider if we want scrolling
setExtent(textWidth, textHeight);
setVirtualExtent(maxVirtualWidth, textHeight);
}
public void fieldChanged(Field f, int context) {
if (f == basicEditField) {
// recalculate how much virtual width the edit field needs, based on the
// current text content
int newWidth = basicEditField.getFont().getAdvance(basicEditField.getText());
setVirtualExtent(newWidth, textHeight);
}
}
}
}
关于java - 水平滚动和 TextField 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10617804/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]