我是Android开发的新手,我已经四处寻找可以合并的代码,但没有运气..
所以我想创建一个Android应用程序,当按下按钮时,它会随机从可绘制文件夹中选择图像,而你不能再按下按钮30秒。
欢迎任何帮助。
Randomactivity.java
package com.example.no;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
public class RandomImage extends Activity implements OnClickListener{
private static final Random rgenerator = new Random();
Integer [] mImageIds = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView iv = (ImageView) findViewById(R.id.imageviewyeah);
iv.setTag(q);
View nextButton = findViewById(R.id.next_image_button);
nextButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_image_button:
Intent i = new Intent(this, RandomImage.class);
startActivity(i);
break;
}
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(this, MainActivity.class));
return true;
}
return false;
}
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget0"
android:background="@drawable/bgi"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<ImageView
android:id="@+id/imageviewyeah"
android:tag="q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</ImageView>
<Button
android:id="@+id/next_image_button"
android:text="Next Image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:typeface="serif"/>
</LinearLayout>
-
答案 0 :(得分:1)
在声明中:
final Random rnd = new Random();
ImageView img = null;
Button btnRandom = null;
在主要活动中:
@Override
protected void onCreate(
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rnd_images);
img = (ImageView) findViewById(R.id.imgRandom);
btnRandom = (Button) findViewById(R.id.btnRandom);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
public void clickHandler(final View v)
{
switch(v.getId())
{
case R.id.btnRandom:
{
if (!btnRandom.isEnabled())
{
return;
}
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
btnRandom.setEnabled(false);
new CountDownTimer(5000, 1000) // Wait 5 secs, tick every 1 sec
{
@Override
public final void onTick(final long millisUntilFinished)
{
btnRandom.setText("Wait: " + (millisUntilFinished / 1000));
}
@Override
public final void onFinish()
{
btnRandom.setText("Change!");
btnRandom.setEnabled(true);
}
}.start();
break;
}
}
}
布局( rnd_images.xml )
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/imgRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<Button
android:id="@+id/btnRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Change!"
android:onClick="clickHandler"
/>
</RelativeLayout>
<强> [编辑] 强>
我修改了布局中的按钮定义。而且我看到时间没有得到尊重,所以,我要解决它。
我还修复了代码以正确处理时序。
<强> [RE-EDIT] 强>
我修改了clickHandler函数以显示剩余的宽限期,并在点击后第一次点燃
答案 1 :(得分:0)
嗯,我唯一能想到的是:
1)从drawable
目录获取所有资源并将它们放入数组或列表
2)有一个随机的生成器编号并将其用作索引从数组/列表中选择一个图像
要获取所有资源,您可能需要找到一种方法来执行此操作。 看了一下这可能会有所帮助:Android: retrieving all Drawable resources from Resources object
一旦你有一个包含所有这些资源的List,你就需要解决你想要的逻辑。
使用此选项生成随机数:How do I generate random integers within a specific range in Java?
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick() {
// assume you put all resources into `resourcesList`
// `randInt` is the method from the link I gave you
resourcesList.get(randInt(0, listSize));
btn.setEnabled(false);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Thread.sleep(30000); // you might need to wrap this with try and catch
return null;
}
@Override
protected void onPostExecute(Void res) {
btn.setEnabled(true);
}
}.execute();
}
});
另一种方法是使用handler.postDelayed
。
Handler h = new Handler();
btn.setOnClickListener(...
public void onClick() {
btn.setEnabled(false);
h.postDelayed(new Runnable() {
@Override
public void run() {
// no need to wrap this in `runOnUiThread` if you create do
// `Handler h = new Handler()` inside a UI thread
btn.setEnabled(true);
}
}, 30000);
}
);
P.S。 AsyncTask可能会造成内存泄漏:Android: Memory leak due to AsyncTask。不确定Handler
,你必须做一些关于它的研究
答案 2 :(得分:0)