我有一个Android程序,我有多个按钮使用相同的OnClickListener,我希望能够从监听器内部引用按钮的动态分配文本.有没有办法引用按下来获取其文本的按钮?我不想让多个特定于按钮的侦听器执行相同的操作.
我正在编写一个Android应用程序,我在一个视图上有8个按钮,它们都具有相同的功能,所以我想使用for循环为每个按钮分配相同的功能,而不是写出8个单独的代码.但是,当我想在onClick函数中使用for循环中的计数器来帮助触发intent时,会出现一个问题,这里是代码:
//array of button ids
public int [] pickPlayers = { R.id.pick_player_1a, R.id.pick_player_2a, R.id.pick_player_3a, R.id.pick_player_4a, R.id.pick_player_1b, R.id.pick_player_2b, R.id.pick_player_3b, R.id.pick_player_4b};
//button to be used in for loop
public Button b;
//for loop to assign same functionality to buttons in pickPlayers array
for(int i = 0; i<pickPlayers.length; i++){
b = (Button) findViewById(pickPlayers[i]);
b.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(getContactIntent, i);
}
});
};
Run Code Online (Sandbox Code Playgroud)
希望所有人都有意义,感谢任何可以提供帮助的人.:)
编辑:这是我得到的错误:不能引用在另一个方法中定义的内部类中的非final变量i
for循环在我的oncreate方法中,变量和按钮在外面
我有一个类似战舰的程序,其中有一个10 x 10的网格按钮。在程序开始时,我希望所有按钮的文本都更改为“ ---”,这表明没有人在该坐标处射击。我似乎找不到一种在一个循环中重命名所有按钮的方法。所有按钮的名称均为b00,b01,b02 ...,显示其坐标。第一个数字是行,第二个数字是列。(即b69代表第7行,第10列)。
希望能对您有所帮助!
先感谢您!
路加
我需要的是简单的:qml方式创建一个按钮,可以在每个平台应用程序上生成本机编译到?(主要是我想知道linux,windows和Mac OS X)
background当点击另一个按钮时,我尝试更改一个按钮.
如果我提供按钮样式,我不能这样做.
请参阅下面的代码.
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="borderTemplate" Property="Border.BorderBrush" Value="Gray" />
<Setter TargetName="borderTemplate" Property="Border.BorderThickness" Value="1" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="borderTemplate" Property="Border.BorderBrush" Value="Lime" />
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter TargetName="borderTemplate" Property="Border.Background" Value="#FD7" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="borderTemplate" Property="Border.Background" Value="LightGray"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button …Run Code Online (Sandbox Code Playgroud) 可以这样写吗?
<a href="add-lead-new.php" target="rightframe"><input type="button" value="New booking" /></a>
Run Code Online (Sandbox Code Playgroud)
该链接应该看起来像一个按钮,但它应该在页面的右侧部分打开.如果错了,还有其他办法吗?上面的代码工作正常.我只是不知道它是否是正确的方法.谢谢
这是我在这里的第一篇文章,所以我希望我提供所有正确的信息.
我目前正在开发一个简单的菜单应用程序,它具有一个按钮控制数组(使用MSDN建议的控制数组的工作),我很难重新调整表单的大小并使按钮居中.这些按钮是在编译时创建的(使用INI文件中的参数),我当前的居中算法似乎稍微将它们设置为右侧.我正在使用"button.location = new Point(...,...)"方法,但在阅读完之后,它表示值将按钮定位在左上角而不是中心,从而占据右侧的轻微偏移.
我的两个问题是:
如何在编译时将这些按钮完美地置于中心位置?我已经尝试通过减去按钮宽度的一半来计算差异但按钮宽度和点属性似乎不兼容并且按钮得到大量偏移.
而且......重新调整菜单大小的另一个目标是在重新调整大小时让按钮完美展开和收缩.当动态创建按钮时似乎锚定无效,所以我被迫编写比率算法......有没有办法让锚定工作?
这是我在加载中的内容:
MyControlArray(i).Location = New Point(CInt(((Width - ButtonWidth) / 2) +_
(ButtonWidth / 2)), CInt((Height - MyControlArray(i).Height) / 2))
Run Code Online (Sandbox Code Playgroud)
我已经尝试过了:
New Point(CInt(((Width - ButtonWidth) / 2)), CInt((Height - MyControlArray(i).Height)_
/ 2))
Run Code Online (Sandbox Code Playgroud) 我想用以下JavaScript创建一个按钮...
var button = document.createElement('button');
button.type = 'button';
button.appendChild(document.createTextNode('I am button'));
document.body.appendChild(button);
Run Code Online (Sandbox Code Playgroud)
它工作得很好,除了IE7/8(我到目前为止所测试的所有内容).
消息:
Object doesn't support this action
行:185
字符:9
代码:0
URI:http://example.com/widget.js
我发现了一个解决方法......
document.body.innerHTML = '<button type="button">I am button</button>';
Run Code Online (Sandbox Code Playgroud)
也就是说,设置innerHTML并让浏览器进行解析.
有没有其他方法可以在没有设置innerHTML属性的情况下在IE中使用它?
我使用jquery mobile生成我的按钮,我在下面有一个CSS代码来改变它的设计,但无论我在宽度和高度上放置什么值,按钮的大小都不会改变,但它相对依赖于字体大小标签.如何更改此设置,以便无论标签的字体大小如何,我都可以更改按钮大小?
$("#ui-1").css({'position':'absolute','left':'27.0px','top':'51.0px','width':'120.0px','height':'30.0px',});
$("#ui-1 .ui-btn-text").css({'font-family':'"Calibri","Arial","Sans Serif"','font-size':'0.75em','color':'black','text-align':'left','wordwrap':'normal','text-shadow':'1px 1px 1px #FFFFFF'});
Run Code Online (Sandbox Code Playgroud) 我需要帮助在循环中不断检查剪贴板或复制文本.基本上我有一个粘贴按钮,但我只想在我将文本复制到剪贴板时启用它,并在我没有复制任何文本时禁用它.我知道我必须使用:
Clipboard.ContainsText
Run Code Online (Sandbox Code Playgroud)
检查我是否有文本复制到我的剪贴板,但我需要帮助制作循环或东西总是检查.
谢谢.