触摸视图时触发错误的android onClickListener

时间:2013-08-12 10:01:30

标签: android onclicklistener

我在onClickListeners上遇到了一些麻烦,我已经添加到两个文本视图中,这两个文本视图是我正在处理的应用程序中的辅助功能栏的一部分。当应用程序在平板电脑上时,监听器会按预期运行并正确地增大和减小文本大小,但是当在手机上运行时,无论触摸哪个视图,都会调用增大大小的监听器。

如果有人能指出我正确的方向,我将非常感激,下面是添加两个听众的代码。

            accLayout = new LinearLayout(this);
    accLayout.setOrientation(LinearLayout.HORIZONTAL);

    TextView bigger = new TextView(this);
    bigger.setBackgroundResource(R.drawable.bigger);
    bigger.setLayoutParams(new LayoutParams(screenWidth/5, utils.dpToPx(60)));
    bigger.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             htmlLayout.increaseTextSize();

        }
    });

    TextView smaller = new TextView(this);
    smaller.setBackgroundResource(R.drawable.smaller);
    smaller.setLayoutParams(new LayoutParams(screenWidth/5, utils.dpToPx(60)));
    smaller.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
              htmlLayout.decreaseTextSize();
        }
    });

    TextView invert = new TextView(this);
    invert.setBackgroundResource(R.drawable.invert);
    invert.setLayoutParams(new LayoutParams(screenWidth/5, utils.dpToPx(60)));
    invert.setHeight(utils.dpToPx(60));
    invert.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!HtmlLayout.INVERTED){

                 htmlLayout.invertColours();
                 HtmlLayout.INVERTED = true;
            } else {

                 htmlLayout.unInvertColours();
                 HtmlLayout.INVERTED = false;
            }

        }
    });     
    LinearLayout sliderContainer = new LinearLayout(this);
    sliderContainer.setBackgroundResource(R.drawable.slider);
    sliderContainer.setLayoutParams(new LayoutParams((screenWidth/5)*2, utils.dpToPx(60)));
    sliderContainer.setGravity(Gravity.CENTER_VERTICAL);

    SeekBar brightness = new SeekBar(this);
    brightness.setLayoutParams(new LayoutParams((int) (screenWidth * 0.4), utils.dpToPx(30)));
    brightness.setMax(100);
    brightness.setBackgroundColor(Color.TRANSPARENT);
    brightness.setProgress((int) (0.7 * 100));
    brightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = (float)(progress + 20) / 100.0f;
            getWindow().setAttributes(lp);

        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub

        }
    });
    sliderContainer.addView(brightness);

    accLayout.setGravity(Gravity.CENTER_VERTICAL);

    accLayout.addView(bigger);
    accLayout.addView(smaller);
    accLayout.addView(invert);
    accLayout.addView(sliderContainer);
    mainContainer.addView(accLayout,2);

1 个答案:

答案 0 :(得分:0)

代码本身看起来正确。您可以尝试更改每个视图的背景,假设它们是相同的颜色,以确切了解它们在屏幕上的显示方式。

我没有我的IDE来测试它,但我怀疑它们可能没有按照你的意图进行布局。