Android找到id返回旧视图

时间:2017-04-09 22:22:32

标签: android timer

我尝试在Android中实现一个计时器,虽然它没有按预期完全正常工作。

我有以下代码用于计时器到期时(位于其自己的方法中):

Button b = (Button) this.findViewById(R.id.actionButton);
        if(b != null && (!b.isEnabled() || b.getText().toString() == "Loading.."))
        {
            Toast.makeText(this, "Ad failed. Loading next ad...", Toast.LENGTH_SHORT).show();
            SetupNextAdd(true); // The ad failed
        }

该按钮是一个只显示广告的按钮,因此用户可以获得奖励。简单吧?

当广告在后台加载完成后,该按钮即可启用,以便在用户点击时为其准备好一些内容。

计时器是要仔细检查广告是否已加载。如果广告失败,我会知道,因为该按钮仍会被停用,或者文字仍会加载。

如果按钮已启用,我希望计时器完成其过程并且是免费的。

我认为这很简单。创建一个漂亮的计时器选中按钮,如果需要,重新加载广告。完成。

但问题是返回的按钮仍然表示文本正在加载并且已被禁用。我知道按钮并不是真的,因为我可以看到屏幕上的按钮既没有禁用也没有加载文本。那么为什么id会返回一个被禁用的按钮?它就像它在更改之前返回旧按钮一样。

这也会导致无限循环,因为返回的按钮状态永远不会与屏幕按钮状态匹配。

那么我怎样才能让计时器以我期望的方式工作?为什么它会返回旧按钮?

这是一个显示问题的完整示例。

查看:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/actionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="350dp"
        android:background="@android:color/holo_blue_bright"
        android:onClick="ShowAdd"
        android:padding="5dp"
        android:text="Loading..."
        android:enabled="false"/>
    <RelativeLayout
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
    </RelativeLayout>

</LinearLayout>

Java代码:

public class MainActivity extends AppCompatActivity {

    private InterstitialAd mInterstitialAd;
    private RewardedVideoAd mAd;
    private AdView mAdView;
    private CountDownTimer timer;

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

        timer = new CountDownTimer(15000, 5000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                TimerDone();
            }
        };

        SetupNextAdd();
        ShowBannerAd();
    }

    public void TimerDone()
    {
        Button b = (Button) findViewById(R.id.actionButton);
        if(b != null && (!b.isEnabled() || b.getText().toString() == "Loading.."))
        {
            Toast.makeText(this, "Ad failed. Loading next ad...", Toast.LENGTH_SHORT).show();
            SetupNextAdd(); // The ad failed
        }
    }

    public void EnableDisableButton(String action)
    {
        MainActivity a = this;
        Button b = (Button)a.findViewById(R.id.actionButton);
        if(action == "enable")
        {
            b.setEnabled(true);
            b.setText("View Ad");
            timer.cancel();
        }
        else
        {
            b.setEnabled(false);
            b.setText("Loading..");
        }
    }
    public void ShowAdd(View view)
    {
        if(mAd.isLoaded()) {
            mAd.show();
        }
    }

    private void SetupNextAdd()
    {
        timer.cancel();
        timer.start();

        if(mAd == null) {
            buildReward();
        }
        requestNewReward();

    }
    private void buildReward()
    {
        mAd = MobileAds.getRewardedVideoAdInstance(this);
        mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
            @Override
            public void onRewardedVideoAdLoaded() {
                EnableDisableButton("enable");
            }

            @Override
            public void onRewardedVideoAdOpened() {

            }

            @Override
            public void onRewardedVideoStarted() {

            }


            @Override
            public void onRewardedVideoAdClosed() {
                EnableDisableButton("disable"); // disable till loaded. No reward for you
            }

            @Override
            public void onRewarded(RewardItem rewardItem) {
                EnableDisableButton("disable");
            }

            @Override
            public void onRewardedVideoAdLeftApplication() {

            }


            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {
                Toast.makeText(getApplicationContext(), "ad failed. Try again.", Toast.LENGTH_SHORT).show();
                EnableDisableButton("disable");
                SetupNextAdd();
            }
        });
    }

    private void requestNewReward()
    {
        mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
    }

    private void ShowBannerAd()
    {
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

0 个答案:

没有答案
相关问题