当前位置:编程学习 > wap >>

循环显示的问题,求指点。(急用)

界面是这样的:四个EditText。分别为:et1\et2\et3\et4 四个控件的背景图都是one.png。还有一个button按钮btn。
我想实现的功能是:点击btn,et1\et2\et3\et4的背景图会无限循环改变成two.png,时间间隔1秒。直到再按一次按钮。two.png显示在循环到的那个EditText控件背景上。希望给段详细的代码,刚开始学android,作业要求,急用谢谢。 --------------------编程问答-------------------- 首先XML里面设置edittext的background为one.png

设置一个循环位  flag = false 开始的时候
设置一个记录位,position = 0 记录当前改变的edittext 的位置
点击button 的时候 void dosomethings()

dosomethings()

    flag = !flag
    while(flags) {
        position = setbackground(position)//设置背景
        Thread.sleep(1000);
    }
}
setbackground(int x)
{
    edittext[x].setbackground(two.png)
    edittext((x-1)%4).setbackground(one.png);
    x++;
    return x;
}

//上面的伪代码是个很不好的方案,因为Thread.sleep 不是太好

你可以 搜一下 handle的用法 new handle  然后 handle。senddelaymessage(update,1000);
update同样要做setbackground里面的工作。希望对你有帮助 --------------------编程问答-------------------- 上面的 x++ 改为 x = (x+1) % 4; --------------------编程问答--------------------
引用 2 楼 shen332401890 的回复:
上面的 x++ 改为 x = (x+1) % 4;

请问直到再按一次按钮。two.png显示在循环到的那个EditText控件背景上。这个如何实现 --------------------编程问答--------------------  flag = !flag 第二次点的时候 flag 等于false 循环就停止了 --------------------编程问答--------------------
引用 2 楼 shen332401890 的回复:
上面的 x++ 改为 x = (x+1) % 4;


引用 1 楼 shen332401890 的回复:
首先XML里面设置edittext的background为one.png

设置一个循环位  flag = false 开始的时候
设置一个记录位,position = 0 记录当前改变的edittext 的位置
点击button 的时候 void dosomethings()

dosomethings()

    flag = !flag
    while(flags) {
        position = setbackground(position)//设置背景
        Thread.sleep(1000);
    }
}
setbackground(int x)
{
    edittext[x].setbackground(two.png)
    edittext((x-1)%4).setbackground(one.png);
    x++;
    return x;
}

//上面的伪代码是个很不好的方案,因为Thread.sleep 不是太好

你可以 搜一下 handle的用法 new handle  然后 handle。senddelaymessage(update,1000);
update同样要做setbackground里面的工作。希望对你有帮助

其实我刚才说的意思错了,是按住和放开按钮,是用onTouch么?如果是的话那样代码应该如何来排?还有就是伪代码不好,是什么意思?不好意思,作为一个新手有太多问题想弄明白了 --------------------编程问答-------------------- 如果 是按住和放开的 ontouch 可以  那样的话 down的时候flag = true 、 up的时候 flag = false 其实是一样的 。 建议你用handle 来做这个东西 thread.sleep 这个我有点偷懒了 --------------------编程问答--------------------
引用 6 楼 shen332401890 的回复:
如果 是按住和放开的 ontouch 可以  那样的话 down的时候flag = true 、 up的时候 flag = false 其实是一样的 。 建议你用handle 来做这个东西 thread.sleep 这个我有点偷懒了

我试了你的代码之后有错,就是在setbackground(int x)这,()里面提示“标记上具有语法错误,错误放置了构造” --------------------编程问答--------------------
引用 6 楼 shen332401890 的回复:
如果 是按住和放开的 ontouch 可以  那样的话 down的时候flag = true 、 up的时候 flag = false 其实是一样的 。 建议你用handle 来做这个东西 thread.sleep 这个我有点偷懒了


btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
dosomethings();

    flag = !flag;
    while(flags) {
        position = setbackground(position);
        Thread.sleep(1000);
    }
}
setbackground(int i) {
EditText[i].setbackground(R.drawable.two);
EditText((i-1)%4).setbackground(R.drawable.one);
    i = (i+1) % 4;
return i;
}
}
});
}
我的代码是这样的。就是在那出错 --------------------编程问答-------------------- setBackgroundResource(int) 试试用这个函数加载你的资源 --------------------编程问答--------------------
引用 9 楼 shen332401890 的回复:
setBackgroundResource(int) 试试用这个函数加载你的资源

无法实现,不过还是要谢谢你。 --------------------编程问答-------------------- package com.example.frameanimationpro;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView image;
private Button btn;
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.btn);
final AnimationDrawable frameAnimation = (AnimationDrawable) image.getBackground();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(flag){
image.setImageBitmap(null);
frameAnimation.start();
flag = false;
}else{
frameAnimation.stop();
image.setImageResource(R.drawable.bg2);
flag = true;

}
}
});

}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<ImageView 
    android:id="@+id/image"
    android:layout_width ="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/animation_list"
    />
<Button 
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click"
    />
</LinearLayout>

res/drawable/animation_list.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/selected" android:oneshot="false" >
   <item android:drawable="@drawable/ic_launcher" android:duration="1000" />
    <item android:drawable="@drawable/bg2" android:duration="1000" />
    <item android:drawable="@drawable/blue_btn" android:duration="1000" />
    <item android:drawable="@drawable/bottom_bar" android:duration="1000" />
    <item android:drawable="@drawable/btn_camera_arrow_left_default" android:duration="1000" />
    <item android:drawable="@drawable/btn_camera_arrow_left_press" android:duration="1000" />

</animation-list>
--------------------编程问答-------------------- 使用FrameAnimation帧布局来实现动画的效果 --------------------编程问答-------------------- package com.example.fortest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

ImageView imageviews[] = new ImageView[4];
Button button;
boolean flags = false;
int position = 0;
Handler myHandle = new Handler(){

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if(flags){
Toast.makeText(getApplicationContext(), "aaa"+position, 0).show();
position = setBackground(position);
myHandle.sendEmptyMessageDelayed(0, 1000);
}else{
myHandle.removeMessages(0);
}
break;

default:
break;
}
super.handleMessage(msg);
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_All_Views();
}

private void init_All_Views() {
imageviews[0] = (ImageView)findViewById(R.id.imageView1);
imageviews[1] = (ImageView)findViewById(R.id.imageView2);
imageviews[2] = (ImageView)findViewById(R.id.imageView3);
imageviews[3] = (ImageView)findViewById(R.id.imageView4);
button = (Button)findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dosomethings();
}
});
}

protected void dosomethings() {
flags = !flags;
myHandle.sendEmptyMessage(0);
}

private int setBackground(int x) {
imageviews[x].setImageResource(R.drawable.ak);
imageviews[(x-1+4)%4].setImageResource(R.drawable.ic_launcher);
x= (x+1)%4;
return x;

}


}


xml

<LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click" />

</LinearLayout>

成功测试 --------------------编程问答--------------------
引用 13 楼 shen332401890 的回复:
package com.example.fortest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

ImageView imageviews[] = new ImageView[4];
Button button;
boolean flags = false;
int position = 0;
Handler myHandle = new Handler(){

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if(flags){
Toast.makeText(getApplicationContext(), "aaa"+position, 0).show();
position = setBackground(position);
myHandle.sendEmptyMessageDelayed(0, 1000);
}else{
myHandle.removeMessages(0);
}
break;

default:
break;
}
super.handleMessage(msg);
}

};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_All_Views();
}

private void init_All_Views() {
imageviews[0] = (ImageView)findViewById(R.id.imageView1);
imageviews[1] = (ImageView)findViewById(R.id.imageView2);
imageviews[2] = (ImageView)findViewById(R.id.imageView3);
imageviews[3] = (ImageView)findViewById(R.id.imageView4);
button = (Button)findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dosomethings();
}
});
}

protected void dosomethings() {
flags = !flags;
myHandle.sendEmptyMessage(0);
}

private int setBackground(int x) {
imageviews[x].setImageResource(R.drawable.ak);
imageviews[(x-1+4)%4].setImageResource(R.drawable.ic_launcher);
x= (x+1)%4;
return x;

}


}


xml

<LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click" />

</LinearLayout>

成功测试


我是想改变edittext的背景 --------------------编程问答-------------------- 那你的
 android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
或许要设置成数值了。。代码的操作逻辑是相同的。没什么问题 帅哥接结贴给分吧,求红花
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,