我刚接触到android和ive做了一些小活动,显示可以拖放到适当的占位符上的形状,如果它们与形状保持匹配,如果它没有它回到原来的位置,但我无法弄清楚如何判断视图组是否为空,因为必须声明null并且不是它自己的状态如果我已经得到了正确的任何人可以帮助所有im之后是一个简单的类型,if / else viewgroup == null do .... animation有人可以为我阐明这个吗?
这是我的
public class PuzzleActivity extends Activity implements OnTouchListener, OnDragListener {
View topLevelLayout;
HorizontalScrollView from;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_puzzle);
findViewById(R.id.squareImage).setOnTouchListener(this);
findViewById(R.id.circleImage).setOnTouchListener(this);
findViewById(R.id.triangleImage).setOnTouchListener(this);
findViewById(R.id.triangleImage2).setOnTouchListener(this);
findViewById(R.id.squareImage1).setOnDragListener(this);
findViewById(R.id.circleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage3).setOnDragListener(this);
topLevelLayout = findViewById(R.id.top_layout);
from = (HorizontalScrollView) findViewById(R.id.scrollView) ;
if (isFirstTime()) {
topLevelLayout.setVisibility(View.INVISIBLE);
}
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if (from.getChildCount() == 1){ Toast zToast = Toast.makeText(getApplicationContext(),
"YOU GENIUS :-)", Toast.LENGTH_SHORT); zToast.show(); }
if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
{
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.topleftcoat);
Toast myToast = Toast.makeText(getApplicationContext(),
"Well Done! :-)", Toast.LENGTH_SHORT);
myToast.show();
return true;
} else if(view.getId()==R.id.circleImage && v.getId()==R.id.circleImage1){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.toprightcoat);
Toast aToast = Toast.makeText(getApplicationContext(),
"Well Done! :-)", Toast.LENGTH_SHORT);
aToast.show();
return true;
} else if(view.getId()==R.id.triangleImage && v.getId()==R.id.triangleImage1){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.bottomleftcoat);
Toast bToast = Toast.makeText(getApplicationContext(),
"Well Done! :-)", Toast.LENGTH_SHORT);
bToast.show();
return true;
}else if(view.getId()==R.id.triangleImage2 && v.getId()==R.id.triangleImage3){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.bottomrightcoat);
Toast cToast = Toast.makeText(getApplicationContext(),
"Well Done! :-)", Toast.LENGTH_SHORT);
cToast.show();
return true;
}
else {
Toast newToast = Toast.makeText(getApplicationContext(),
"Oops! Try again :-)", Toast.LENGTH_SHORT);
newToast.show();
return true;
}
}
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.reset:
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
public void onClick2(View view){
Intent intent = new Intent(this, PuzzleActivity2.class);
finish();
startActivity(intent);
}
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLevelLayout.setVisibility(View.VISIBLE);
topLevelLayout.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
topLevelLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
}
答案 0 :(得分:0)
您可以在ViewGroup实例上使用getChildCount()方法来确定剩余的子项数。