我在Android中构建了一个简单的音乐播放器.每首歌曲的视图都包含一个SeekBar,实现方式如下:
public class Song extends Activity implements OnClickListener,Runnable {
private SeekBar progress;
private MediaPlayer mp;
// ...
private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
progress.setVisibility(SeekBar.VISIBLE);
progress.setProgress(0);
mp = appService.getMP();
appService.playSong(title);
progress.setMax(mp.getDuration());
new Thread(Song.this).start();
}
public void onServiceDisconnected(ComponentName classname) {
appService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
// ...
progress = (SeekBar) findViewById(R.id.progress);
// ...
}
public void run() { …
Run Code Online (Sandbox Code Playgroud) 我有一系列哈希:
a=[{ 'foo'=>0,'bar'=>1 },
{ 'foo'=>0,'bar'=>2 },
... ]
Run Code Online (Sandbox Code Playgroud)
我想先按每个哈希的'foo'排序数组,然后按'bar'排序.谷歌告诉我这就是它的完成方式:
a.sort_by {|h| [ h['foo'],h['bar'] ]}
Run Code Online (Sandbox Code Playgroud)
但是这给了我ArgumentError"Array与Array的比较失败".这是什么意思?
我有一个包含一对并排按钮的RelativeLayout,我想在布局中居中.我可以将按钮放在LinearLayout中并将其放在RelativeLayout中,但我希望尽可能保持我的xml干净.
这是我尝试过的,这只是将"apply"按钮放在中间,将"undo"按钮放在它的左边:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp">
<TextView
android:id="@+id/instructions"
android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/apply"
android:textSize="20sp"
android:layout_below="@id/instructions"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/undo"
android:textSize="20sp"
android:layout_toLeftOf="@id/apply"
android:layout_below="@id/instructions"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我制作了一个简单的Android音乐播放器.我希望有一个TextView,以分钟为单位显示歌曲中的当前时间:秒格式.所以我尝试的第一件事就是让活动Runnable并把它放在run()中:
int position = 0;
while (MPService.getMP() != null && position<MPService.duration) {
try {
Thread.sleep(1000);
position = MPService.getSongPosition();
} catch (InterruptedException e) {
return;
}
// ... convert position to formatted minutes:seconds string ...
currentTime.setText(time); // currentTime = (TextView) findViewById(R.id.current_time);
Run Code Online (Sandbox Code Playgroud)
但这失败了因为我只能在创建它的线程中触摸TextView.所以我尝试使用runOnUiThread(),但是这不起作用,因为在主线程上重复调用Thread.sleep(1000),因此活动只挂在空白屏幕上.那么我有什么想法可以解决这个问题?
新代码:
private int startTime = 0;
private Handler timeHandler = new Handler();
private Runnable updateTime = new Runnable() {
public void run() {
final int start = startTime;
int millis = appService.getSongPosition() - start;
int seconds = (int) ((millis …
Run Code Online (Sandbox Code Playgroud) float minX = 50.0f;
float maxX = 100.0f;
Random rand = new Random();
float finalX = rand.nextFloat(maxX - minX + 1.0f) + minX;
Run Code Online (Sandbox Code Playgroud)
"Random类型的方法nextFloat()不适用于参数(float)"
嗯什么?
当我尝试运行我的Android项目(昨天工作正常)时,我得到"您的项目包含错误,请在运行应用程序之前修复它们"对话框.但我的项目没有错误.我之前遇到过这个问题,而且解决方案始终是清理项目,但这次没有帮助.红色X图标从工作区中的项目中消失,然后在我尝试运行时重新出现.还重新启动了Eclipse,以便重建项目.救命?
我想要做的很简单:
f=Foobar.objects.get(id=1)
foo='somefield'
bar='somevalue'
f.foo=bar
f.save()
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它试图更新f对象的'foo'字段,当然这不存在.我怎么能做到这一点?
我所说的突出显示是指当您将鼠标拖动到文本上时对文本所做的操作。如果你使用 imgur.com 那么你就知道我想要什么。我在任何地方都找不到任何关于此的信息,这令人沮丧。帮助?
编辑:好吧,我以为我已经说得足够清楚了,但我想还不够。我并不是说我想改变悬停时的背景颜色。那是微不足道的。但是您知道当页面上有文本时,您单击文本并拖动鼠标,或者按 ctrl+A,背景颜色会发生变化,然后您可以复制文本吗?你知道吗,突出显示?选择?我不希望它看起来像是通过更改背景颜色而发生的,我希望它确实发生。在 imgur.com 上上传一张图片,您就会明白我的意思。请注意,当您将鼠标悬停在上传图像的任何链接上时,文本将被选中 - 您可以复制它。
这就是为什么很难找到有关此的任何信息。我得到的只是如何更改背景颜色的结果。
我花了最后几个小时将我/ home移动到自己的分区,然后重新安装Ubuntu 10.10.没有问题.我重新安装了Eclipse,一切都应该是这样的:安装了ADT插件,我的工作区设置正确,甚至还记得我上一次会话.但是当我尝试运行Android项目时,我得到了这个:
[2011-05-28 21:28:13 - Game] The connection to adb is down, and a severe error has occured.
[2011-05-28 21:28:13 - Game] You must restart adb and Eclipse.
[2011-05-28 21:28:13 - Game] Please ensure that adb is correctly located at '/home/evan/android-sdk/platform-tools/adb' and can be executed.
Run Code Online (Sandbox Code Playgroud)
adb实际上位于/ home/evan/android-sdk/platform-tools,并且是可执行的.而且它不仅仅是Eclipse:我可以cd到平台工具并运行adb命令(./adb logcat
)而且我得到了bash: ./adb: No such file or directory
.
我重新安装了SDK平台工具,它没有帮助.
strace的:
execve("/home/evan/android-sdk/platform-tools/adb", ["/home/evan/android-sdk/platform-"...], [/* 41 vars */]) = -1 ENOENT (No such file or directory)
dup(2) = 3
fcntl(3, F_GETFL) …
Run Code Online (Sandbox Code Playgroud) 我想从另一个中减去一个整数,结果应该在0处.所以2减4应该等于0.我可以这样做
int result = x - y;
if (result < 0) result = 0;
Run Code Online (Sandbox Code Playgroud)
但是有更优雅的方式吗?