触摸听取边缘的触摸听众

时间:2014-11-06 05:09:48

标签: java android

我正在尝试创建一个简单的程序,从0到9随机生成数字到屏幕,然后当你触摸它时,它们就会消失。我遇到的问题是当我触摸屏幕上的空白区域时,数字会消失。如何防止这种情况发生?这是因为边缘?

MainActivity.java(更新:将onClick更改为populate()内部)

package com.example.textdisappear;

import java.util.Random;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity
{
    private FrameLayout layout;
    private Random rand;
    private int left, top, height, width, textSizeBufferL, textSizeBufferT, counter;
    private Display display;
    private FrameLayout.LayoutParams rlp;

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

        display = getWindowManager().getDefaultDisplay();
        height = display.getHeight();
        width = display.getWidth();
        rand = new Random();
        layout = (FrameLayout)findViewById(R.id.layout);
        textSizeBufferL = width/9;
        textSizeBufferT = height/7;
        counter = 0;
        populateLayout();
    }

    public void populateLayout()
    {
        layout.removeAllViews();
        for(int x = 0; x < 10; x++)
        {
            TextView view = new TextView(this);
            view.setOnClickListener(new View.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    v.setVisibility(View.INVISIBLE);
                    counter++;
                    if(counter >= 10)
                    {
                        counter = 0;
                        populateLayout();
                    }
                }
            });
            rlp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            do
            {
            left = rand.nextInt(width);
            //right = rand.nextInt(width);
            } while(left + textSizeBufferL > width);

            do
            {
            top = rand.nextInt(height);
            //bottom = rand.nextInt(height);
            } while(top + textSizeBufferT > height);

            rlp.setMargins(left, top, 0, 0);
            view.setLayoutParams(rlp);
            view.setText(""+x);
            view.setTextSize(15);
            //view.setId(x+1);
            layout.addView(view);
        }
        int x = 0;
        if(x == 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings)
        {
            return true;
        }
        if (id == R.id.reset)
        {
            counter = 0;
            populateLayout();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<FrameLayout 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"
    tools:context="com.example.textdisappear.MainActivity" >

    <FrameLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

1 个答案:

答案 0 :(得分:1)

  

我遇到的问题是当我触摸屏幕上的空白区域时   数字消失了。

是的,因为您覆盖了在活动中触摸整个屏幕中任何位置时将调用的onTouch方法。

@Override
public boolean onTouch(View v, MotionEvent event)
{
    //move this whole code to onClick for textviews
    v.setVisibility(View.INVISIBLE);
    if(counter < 10)
    {
        counter++;
        return false;
    }
    if(counter == 10)
    {
        counter = 0;
        populateLayout();
        return false;
    }
    return false;
}

如果您希望textview仅在触及textview时消失,请改为使用onClick并设置textview。这样,只有在点击textview整个屏幕时才会调用代码。

更新1

您将onClick设置在错误的位置。您应该执行以下内部 populateLayout

TextView view = new TextView(this);
view .setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
                v.setVisibility(View.INVISIBLE);
    counter++;
    if(counter >= 10)
    {
        counter = 0;
        populateLayout();
    }
    }

});

之后,删除你重写的onClick,并且不需要在Activity中阻止onClickListener:

//delete this code
 @Override
    public void onClick(View v)
    {
        v.setVisibility(View.INVISIBLE);
        counter++;
        if(counter >= 10)
        {
            counter = 0;
            populateLayout();
        }
    }

更新2 并将rlp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);更改为WRAP_CONTENT