以编程方式更改背景

时间:2015-11-05 17:37:50

标签: java android

我有这个代码,当你点击一个按钮改变背景但是,我想每10秒更改一次背景,我想在文件img1.png,img2.png和img3.png之间切换完成后重新开始。提前致谢。 这是代码: 在'MainActivity.java'中

package lucas.app_2001;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Button button;
    LinearLayout mainLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainLayout=(LinearLayout)findViewById(R.id.myLayout);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                mainLayout.setBackgroundResource(R.drawable.yellowgradient);
            }
        });
    }
}

'MainActivity.xml':

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="71dp"
        android:text="Shout!" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

使用while循环重复。

要延迟10秒,请尝试以下方法:

//wait 10 seconds
        Button.postDelayed(new Runnable() {

            @Override
            public void run() {
                Button.setClickable(true);                        
            }
        }, 10000);

答案 1 :(得分:0)

你可以这样做:

private Handler mHandler;
private Runnable mRunnable;
private int i = 0;

<强> MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Button button;
    LinearLayout mainLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainLayout=(LinearLayout)findViewById(R.id.myLayout);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                mainLayout.setBackgroundResource(R.drawable.yellowgradient);
            }
        });

        mHandler = new Handler();
        mRunnable = new Runnable(){
        @Override
        public void run() {
             i++;
             if(i==1){
             mainLayout.setBackgroundResource(R.drawable.image_one);
              }else if(i==2){
             mainLayout.setBackgroundResource(R.drawable.image_two);
              }else if(i==3){
             mainLayout.setBackgroundResource(R.drawable.image_three);
               i ==0;
              }

             mHandler.postDelayed(mRunnable , 10000);
            }
         }; 
        mHandler .post(mRunnable);
    }
}
onStop()

if(mHandler!=null){
  mHandler.removeCallbacks(mRunnable);
}

完成