界面是这样的:四个EditText。分别为:et1\et2\et3\et4 四个控件的背景图都是one.png。还有一个button按钮btn。
我想实现的功能是:点击btn,et1\et2\et3\et4的背景图会无限循环改变成two.png,时间间隔1秒。直到再按一次按钮。two.png显示在循环到的那个EditText控件背景上。希望给段详细的代码,刚开始学android,作业要求,急用谢谢。
我想实现的功能是:点击btn,et1\et2\et3\et4的背景图会无限循环改变成two.png,时间间隔1秒。直到再按一次按钮。two.png显示在循环到的那个EditText控件背景上。希望给段详细的代码,刚开始学android,作业要求,急用谢谢。
设置一个记录位,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么?如果是的话那样代码应该如何来排?还有就是伪代码不好,是什么意思?不好意思,作为一个新手有太多问题想弄明白了
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;
}
}
});
}
我的代码是这样的。就是在那出错
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>
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"
或许要设置成数值了。。代码的操作逻辑是相同的。没什么问题 帅哥接结贴给分吧,求红花