繁体   English   中英

在触摸式监听器上聆听边距

[英]On touch listener listening to the margins

我正在尝试创建一个简单的程序,该程序从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>

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

是的,因为您覆盖了在活动中整个屏幕上任何地方触摸时将调用的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM