gpt4 book ai didi

java - 触摸听者听边缘

转载 作者:行者123 更新时间:2023-11-30 02:34:38 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的程序,它会在屏幕上随机生成 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>

最佳答案

The problem I am having is when I touch white space on the screen the numbers disappear.

是的,因为您覆盖了当您在 Activity 中触摸整个屏幕的任何地方时将调用的 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 设置在错误的位置。你应该在 inside 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

关于java - 触摸听者听边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com