首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab编辑文本框-显示提示?

问Matlab编辑文本框-显示提示?
EN

Stack Overflow用户
提问于 2012-05-06 05:28:21
回答 1查看 3.5K关注 0票数 2

在使用Matlab时,有没有办法在编辑文本框中放置“提示”?也就是说,一旦用户开始键入,文本就会消失?我在Android中使用过类似的功能,但我对其他GUI不太熟悉,所以我不确定这个功能有多普遍。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-07 01:34:49

这在Matlab中是可能的,但您必须定义一个自定义MouseClickCallback,它只能由Yair Altman使用findjobj访问(使用链接从Matlab文件交换下载它,并将其保存在您的Matlab路径中的某个位置)。

因为我喜欢这个想法,所以我写了一个函数,可以方便地处理所有这些问题。它会创建一个灰显的斜体帮助文本,单击编辑框后该文本将消失。

​

​

代码语言:javascript
复制
function setInitialHelp(hEditbox,helpText)
%SETINITIALHELP adds a help text to edit boxes that disappears when the box is clicked
%
% SYNOPSIS: setInitialHelp(hEditbox,helpText)
%
% INPUT hEditbox: handle to edit box. The parent figure cannot be docked, the edit box cannot be part of a panel.
%       helpText: string that should initially appear as help. Optional. If empty, current string is considered the help.
%
% SEE ALSO uicontrol, findjobj
%
% EXAMPLE   
%           fh = figure;
%           % define uicontrol. Set foregroundColor, fontAngle, before 
%           % calling setInitialHelp
%           hEditbox = uicontrol('style','edit','parent',fh,...
%             'units','normalized','position',[0.3 0.45 0.4 0.15],...
%             'foregroundColor','r');
%           setInitialHelp(hEditbox,'click here to edit')
%

% check input
if nargin < 1 || ~ishandle(hEditbox) || ~strcmp(get(hEditbox,'style'),'edit')
    error('please supply a valid edit box handle to setInitialHelp')
end

if nargin < 2 || isempty(helpText)
    helpText = get(hEditbox,'string');
end

% try to get java handle
jEditbox = findjobj(hEditbox,'nomenu');
if isempty(jEditbox)
    error('unable to find java handle. Figure may be docked or edit box may part of panel')
end

% get current settings for everything we'll change
color = get(hEditbox,'foregroundColor');
fontAngle = get(hEditbox,'fontangle');

% define new settings (can be made optional input in the future)
newColor = [0.5 0.5 0.5];
newAngle = 'italic';

% set the help text in the new style
set(hEditbox,'string',helpText,'foregroundColor',newColor,'fontAngle',newAngle)

% add the mouse-click callback
set(jEditbox,'MouseClickedCallback',@(u,v)clearBox());

% define the callback "clearBox" as nested function for convenience
    function clearBox
        %CLEARBOX clears the current edit box if it contains help text

        currentText = get(hEditbox,'string');
        currentColor = get(hEditbox,'foregroundColor');

        if strcmp(currentText,helpText) && all(currentColor == newColor)
            % delete text, reset color/angle
            set(hEditbox,'string','','foregroundColor',color,'fontAngle',fontAngle)
        else
            % this is not help text anymore - don't do anything
        end

    end % nested function

end % main fcn
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10466143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档