改善按钮响应时间

时间:2017-03-23 18:29:43

标签: java android

我有一个主菜单,其中包含许多按钮。如果我按下任何按钮,它将在2/3秒内完成任何操作,而不是加载活动。加载的活动包含一个包含6页或更多页面的viewpager。有人知道我如何提高按钮的性能吗?

这是我的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton button_01, button_02, button_03, button_04, button_05, button_06, button_07, button_08, button_09, button_10, button_11, button_12, button_13;
    private ListView mDrawerList;
    private ArrayAdapter<String> mAdapter;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private String mActivityTitle;
    public static KapitelAnlage kapitelAnlage;
    public static SharedPreferences pref;
    public static SharedPreferences.Editor editor;


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


        pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
        editor = pref.edit();


        kapitelAnlage = new KapitelAnlage();

        button_01 = (ImageButton) findViewById(R.id.button_01);
        button_01.setOnClickListener(this);


        button_02 = (ImageButton) findViewById(R.id.button_02);
        button_02.setOnClickListener(this);


        button_03 = (ImageButton) findViewById(R.id.button_03);
        button_03.setOnClickListener(this);

        button_04 = (ImageButton) findViewById(R.id.button_04);
        button_04.setOnClickListener(this);
        //button_04.setEnabled(false);
        //button_04.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);

        button_05 = (ImageButton) findViewById(R.id.button_05);
        button_05.setOnClickListener(this);
        //button_05.setEnabled(false);
        //button_05.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_06 = (ImageButton) findViewById(R.id.button_06);
        button_06.setOnClickListener(this);
        //button_06.setEnabled(false);
        //button_06.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_07 = (ImageButton) findViewById(R.id.button_07);
        button_07.setOnClickListener(this);
        //button_07.setEnabled(false);
        //button_07.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);

        button_08 = (ImageButton) findViewById(R.id.button_08);
        button_08.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_09 = (ImageButton) findViewById(R.id.button_09);
        button_09.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_10 = (ImageButton) findViewById(R.id.button_10);
        button_10.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_11 = (ImageButton) findViewById(R.id.button_11);
        button_11.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        button_12 = (ImageButton) findViewById(R.id.button_12);
        button_12.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);

        button_13 = (ImageButton) findViewById(R.id.button_13);
        button_13.setOnClickListener(this);
        //button_08.setEnabled(false);
        //button_08.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);


        buttonAktiviere();
        mDrawerList = (ListView) findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

    @Override
    public void onClick(View v) {

        if (v == button_01) {

            Intent intent = new Intent(this, Kapitel_01.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_01);

        } else if (v == button_02) {
            Intent intent = new Intent(this, Kapitel_02.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_02);

        } else if (v == button_03) {
            Intent intent = new Intent(this, Kapitel_03.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_03);

        } else if (v == button_04) {
            Intent intent = new Intent(this, Kapitel_04.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_04);
        } else if (v == button_05) {
            Intent intent = new Intent(this, Kapitel_05.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_05);
        } else if (v == button_06) {
            Intent intent = new Intent(this, Kapitel_06.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_06);
        } else if (v == button_07) {
            Intent intent = new Intent(this, Kapitel_07.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_07);
        } else if (v == button_08) {
            Intent intent = new Intent(this, Kapitel_08.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_08);
        } else if (v == button_09) {
            Intent intent = new Intent(this, Kapitel_09.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_09);
        } else if (v == button_10) {
            Intent intent = new Intent(this, Kapitel_10.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_10);
        } else if (v == button_11) {
            Intent intent = new Intent(this, Kapitel_11.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_11);
        } else if (v == button_12) {
            Intent intent = new Intent(this, Kapitel_12.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_12);
        } else if (v == button_13) {
            Intent intent = new Intent(this, Kapitel_13.class);
            this.startActivity(intent);
            kapitelAnlage.setKapitel(Kapitel.Kapitel_13);
        }
    }


    private void addDrawerItems() {
        String[] osArray = {"Aktien", "Renten", "Immobilien", "Blog", "Kontakt"};
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupDrawer() {

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close) {


            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Navigation!");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.optionsmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        } else if (item.getItemId() == R.id.item2) {
            Intent intent = new Intent(this, Impressum.class);
            this.startActivity(intent);
        }

        return super.onOptionsItemSelected(item);
    }


    public void buttonAktiviere() {


        if (checkKap("Kap03")) {
            buttonEnable(button_04);
            Log.d("Meine APP", "Kapitel 4 ist freigeschaltet");


        }

    }


    public boolean checkKap(String kap) {

        boolean check = pref.getBoolean(kap, false);
        if (check) {
            return true;
        } else return false;


    }


    public void buttonEnable(ImageButton btn) {
        btn.getBackground().setColorFilter(null);
        btn.setEnabled(true);
    }



}

public class Kapitel_01 extends Activity {

    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;
    private LinearLayout dotsLayout;
    private TextView[] dots;
    private int[] layouts;
    private Button btnNext;
    private ImageView image_1_a, image_1_b;
    private WebView header_1_a,text_1_a;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        setContentView(R.layout.activity_slider);



        layouts = new int[]{
                R.layout.slide_type_a,
                R.layout.slide_type_a,
                R.layout.slide_type_c,
                R.layout.slide_type_a,
                R.layout.slide_type_a,
                R.layout.slide_type_b,
                R.layout.slide_type_b,
                R.layout.slide_type_b,
                R.layout.slide_type_b,
                R.layout.startquiz_layout};

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setOffscreenPageLimit(Setting.getOffscreenSet(layouts.length));
        dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
        btnNext = (Button) findViewById(R.id.btn_next);


        // adding bottom dots
        addBottomDots(0);

        viewPagerAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener);


    }

    public void btnQuizStart(View v) {
        Intent intent = new Intent(this, Quiz.class);
        image_1_a = (ImageView) findViewById(R.id.image_slide_type_a_1);
        this.startActivity(intent);
    }


    public void btnNextClick(View v) {
        // checking for last page
        // if last page home screen will be launched
        int current = getItem(1);
        if (current < layouts.length) {
            // move to next screen
            viewPager.setCurrentItem(current);
        } else {
            launchHomeScreen();
        }
    }


    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            addBottomDots(position);

            // changing the next button text 'NEXT' / 'GOT IT'
            if (position == layouts.length - 1) {
                // last page. make button text to GOT IT
                btnNext.setText(getString(R.string.start));

            } else {
                // still pages are left
                btnNext.setText(getString(R.string.next));
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };


    private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(this);
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(getResources().getColor(R.color.inactive_dots));
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(getResources().getColor(R.color.active_dots));
    }


    private int getItem(int i) {


        return viewPager.getCurrentItem() + i;
    }

    private void launchHomeScreen() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }


    public class ViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;


        public ViewPagerAdapter() {

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {



            layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            HTMLPre preparer = new HTMLPre();



            String font = "SourceSansPro-Regular";

            View one_one = layoutInflater.inflate(R.layout.slide_type_a, container, false);
            final LinearLayout layout = (LinearLayout) one_one.findViewById(R.id.loadscreen);
            layout.setVisibility(View.VISIBLE);
            dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
            dotsLayout.setVisibility(View.GONE);



            WebView header_1_a = (WebView) one_one.findViewById(R.id.header_slide_type_a_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie9_header), header_1_a, font);
            image_1_a = (ImageView) one_one.findViewById(R.id.image_slide_type_a_1);
            image_1_a.setImageResource(R.drawable.picture_kap01_01);
            WebView text_1_a = (WebView) one_one.findViewById(R.id.text_slide_type_a_1);
            preparer.loadHTLMContentText(getString(R.string.Historie9), text_1_a, font);


            View one = layoutInflater.inflate(R.layout.slide_type_a, container, false);


            header_1_a = (WebView) one.findViewById(R.id.header_slide_type_a_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie1_header), header_1_a, font);
            image_1_a = (ImageView) one.findViewById(R.id.image_slide_type_a_1);
            image_1_a.setImageResource(R.drawable.picture_kap01_01);
            text_1_a = (WebView) one.findViewById(R.id.text_slide_type_a_1);
            preparer.loadHTLMContentText(getString(R.string.Historie1), text_1_a, font);


            View two = layoutInflater.inflate(R.layout.slide_type_c, container, false);

            image_1_a = (ImageView) two.findViewById(R.id.image_slide_type_c_1);
            image_1_a.setImageResource(R.drawable.boerse_antwerpen);
            text_1_a = (WebView) two.findViewById(R.id.text_slide_type_c_1);
            preparer.loadHTLMContentText(getString(R.string.Historie2), text_1_a, font);


            View three = layoutInflater.inflate(R.layout.slide_type_a, container, false);

            header_1_a = (WebView) three.findViewById(R.id.header_slide_type_a_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie3_header), header_1_a, font);
            image_1_a = (ImageView) three.findViewById(R.id.image_slide_type_a_1);
            image_1_a.setImageResource(R.drawable.parketthandel);
            text_1_a = (WebView) three.findViewById(R.id.text_slide_type_a_1);
            preparer.loadHTLMContentText(getString(R.string.Historie3), text_1_a, font);


            View four = layoutInflater.inflate(R.layout.slide_type_a, container, false);

            header_1_a = (WebView) four.findViewById(R.id.header_slide_type_a_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie4_header), header_1_a, font);
            image_1_a = (ImageView) four.findViewById(R.id.image_slide_type_a_1);
            image_1_a.setImageResource(R.drawable.parketthandel);
            text_1_a = (WebView) four.findViewById(R.id.text_slide_type_a_1);
            preparer.loadHTLMContentText(getString(R.string.Historie4), text_1_a, font);


            View five = layoutInflater.inflate(R.layout.slide_type_b, container, false);

            WebView header_1_b = (WebView) five.findViewById(R.id.header_slide_type_b_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie5_header), header_1_b, font);
            View layout_1_b = (View) five.findViewById(R.id.layout_slide_type_b_1);
            layout_1_b.setBackgroundResource(R.drawable.london_old_stock_exchange);


            View six = layoutInflater.inflate(R.layout.slide_type_b, container, false);

            header_1_b = (WebView) six.findViewById(R.id.header_slide_type_b_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie6_header), header_1_b, font);
            layout_1_b = (View) six.findViewById(R.id.layout_slide_type_b_1);
            layout_1_b.setBackgroundResource(R.drawable.frankfurt_old_stock);

            View seven = layoutInflater.inflate(R.layout.slide_type_b, container, false);

            header_1_b = (WebView) seven.findViewById(R.id.header_slide_type_b_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie7_header), header_1_b, font);
            layout_1_b = (View) seven.findViewById(R.id.layout_slide_type_b_1);
            layout_1_b.setBackgroundResource(R.drawable.newyork_stock_old);

            View eight = layoutInflater.inflate(R.layout.slide_type_b, container, false);

            header_1_b = (WebView) eight.findViewById(R.id.header_slide_type_b_1);
            preparer.loadHTLMContentHeader(getString(R.string.Historie8_header), header_1_b, font);
            layout_1_b = (View) eight.findViewById(R.id.layout_slide_type_b_1);
            layout_1_b.setBackgroundResource(R.drawable.tokio_boerse);


            View nine = layoutInflater.inflate(R.layout.startquiz_layout, container, false);
            View viewarr[] = {one_one, one, two, three, four, five, six, seven, eight, nine};
            container.addView(viewarr[position]);
            layout.postDelayed(new Runnable() { public void run() { layout.setVisibility(View.GONE);
            dotsLayout.setVisibility(View.VISIBLE);} }, 3000);


            return viewarr[position];


        }

        @Override
        public int getCount() {
            return layouts.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;

            container.removeView(view);
        }
    }


}

如果按下button_01,mainactivity将加载kapitel_01类。

0 个答案:

没有答案