我试图创建一个简单的程序,它从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>
我改变了它onTouch到的onClick现在,无论我触摸屏幕的位置如何,随机文本视图都会消失。 编辑:它不是随机的。当点击右下角时,它将从9倒数到0的可见性设置为0。如何将其更改为边距不激活点击的位置? – 2014-11-06 05:49:15
@PythonNoob请用您的新代码更新您的问题 – 2014-11-06 05:51:20
@PythonNoob请注意,您应该在Textview中设置onClickListener – 2014-11-06 05:51:53