我试图创建一个简单的程序,随机生成数字从0-9到屏幕,然后当你触摸它,它们消失。我遇到的问题是当我触摸屏幕上的空白时,数字就消失了。我怎样才能防止这种情况发生?这是因为利润率吗?
MainActivity.java (UPDATE:将onClick更改为填充())
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>发布于 2014-11-06 05:16:21
我遇到的问题是当我触摸屏幕上的空白时,数字就消失了。
是的,因为您高估了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,并且不需要在活动中强制执行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
https://stackoverflow.com/questions/26772138
复制相似问题