2015-04-06 66 views
1

我正在尝试进行字母检查。我有一个ImageView11,它显示你必须猜测的随机字母表。 imageView_alphabet_image_1imageView_alphabet_image_2将显示我必须猜测的两个选项,并且我会将ImageView11图像拖动到显示正确的ImageView上。但我能做到这一点只有第一次当我刷新点击它总是显示不正确敬酒无法进行拖放

<LinearLayout 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" 
android:orientation="vertical" > 
<Button 
    android:id="@+id/btn_refresh" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Refresh"/> 

<LinearLayout 
    android:id="@+id/dragLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="50dp" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imgView_des" 
     android:layout_width="70dp" 
     android:layout_height="80dp" 
     android:src="@drawable/ic_launcher" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/bottomLinearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="150dp" 
    android:orientation="horizontal" 
    android:weightSum="1" > 

    <ImageView 
     android:id="@+id/imgView_alphabetImage_1" 
     android:layout_width="70dp" 
     android:layout_height="80dp" 
     android:layout_weight="0.25" 
     android:src="@drawable/a" /> 
    <ImageView 
     android:id="@+id/imgView_alphabetImage_2" 
     android:layout_width="70dp" 
     android:layout_height="80dp" 
     android:layout_weight="0.25" 
     android:src="@drawable/a" /> 
</LinearLayout> 

package com.example.cleardoubt; 
 

 
import java.util.ArrayList; 
 
import java.util.Collections; 
 
import java.util.Random; 
 
import android.os.Bundle; 
 
import android.support.v7.app.ActionBarActivity; 
 
import android.util.Log; 
 
import android.view.DragEvent; 
 
import android.view.Menu; 
 
import android.view.MenuItem; 
 
import android.view.MotionEvent; 
 
import android.view.View; 
 
import android.view.View.DragShadowBuilder; 
 
import android.view.View.OnClickListener; 
 
import android.view.View.OnDragListener; 
 
import android.view.View.OnTouchListener; 
 
import android.view.ViewGroup; 
 
import android.widget.Button; 
 
import android.widget.ImageView; 
 
import android.widget.Toast; 
 

 
public class MainActivity extends ActionBarActivity implements OnClickListener, 
 
\t \t OnTouchListener, OnDragListener { 
 

 
\t private ImageView _imgView_des; 
 
\t private ImageView _imgView_alphabetImage_1; 
 
\t private ArrayList<Integer> _alphabet_arrayList; 
 
\t private Button _btn_refresh; 
 
\t private ImageView _imgView_alphabetImage_2; 
 
\t private ArrayList<Integer> _tempArrayList; 
 
\t private ArrayList<Integer> _finalTempArrayList; 
 

 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t initView(); 
 
\t \t setContentView(R.layout.activity_main); 
 
\t \t _imgView_des = (ImageView) findViewById(R.id.imgView_des); 
 
\t \t _imgView_des.setOnClickListener(this); 
 
\t \t _imgView_des.setOnTouchListener(this); 
 
\t \t _imgView_des.setOnDragListener(this); 
 
\t \t _imgView_alphabetImage_1 = (ImageView) findViewById(R.id.imgView_alphabetImage_1); 
 
\t \t _imgView_alphabetImage_1.setOnClickListener(this); 
 
\t \t _imgView_alphabetImage_1.setOnDragListener(this); 
 
\t \t _imgView_alphabetImage_2 = (ImageView) findViewById(R.id.imgView_alphabetImage_2); 
 
\t \t _imgView_alphabetImage_2.setOnClickListener(this); 
 
\t \t _imgView_alphabetImage_2.setOnDragListener(this); 
 
\t \t _btn_refresh = (Button) findViewById(R.id.btn_refresh); 
 
\t \t _btn_refresh.setOnClickListener(this); 
 
\t } 
 

 
\t private void initView() { 
 
\t \t _alphabet_arrayList = new ArrayList<Integer>(); 
 
\t \t _alphabet_arrayList.add(R.drawable.a); 
 
\t \t _alphabet_arrayList.add(R.drawable.b); 
 
\t \t _alphabet_arrayList.add(R.drawable.c); 
 
\t \t _alphabet_arrayList.add(R.drawable.d); 
 
\t \t _alphabet_arrayList.add(R.drawable.e); 
 
\t \t _alphabet_arrayList.add(R.drawable.f); \t 
 
\t \t _alphabet_arrayList.add(R.drawable.g); \t 
 
\t \t 
 
\t \t _tempArrayList = new ArrayList<Integer>(); 
 
\t \t _finalTempArrayList = new ArrayList<Integer>(); 
 
\t \t 
 
\t } 
 

 
\t @Override 
 
\t public boolean onCreateOptionsMenu(Menu menu) { 
 
\t \t // Inflate the menu; this adds items to the action bar if it is present. 
 
\t \t getMenuInflater().inflate(R.menu.main, menu); 
 
\t \t return true; 
 
\t } 
 

 
\t @Override 
 
\t public boolean onOptionsItemSelected(MenuItem item) { 
 

 
\t \t int id = item.getItemId(); 
 
\t \t if (id == R.id.action_settings) { 
 
\t \t \t return true; 
 
\t \t } 
 
\t \t return super.onOptionsItemSelected(item); 
 
\t } 
 

 
\t @Override 
 
\t public void onClick(View v) { 
 
\t \t switch (v.getId()) { 
 
\t \t case R.id.imgView_des: 
 

 
\t \t \t break; 
 
\t \t case R.id.imgView_alphabetImage_1: 
 
\t \t \t if (_imgView_des 
 
\t \t \t \t \t .getDrawable() 
 
\t \t \t \t \t .getConstantState() 
 
\t \t \t \t \t .equals(_imgView_alphabetImage_1.getDrawable() 
 
\t \t \t \t \t \t \t .getConstantState())) { 
 
\t \t \t \t Toast.makeText(this, "matched", Toast.LENGTH_SHORT).show(); 
 
\t \t \t } else { 
 
\t \t \t \t Toast.makeText(this, "not matched", Toast.LENGTH_SHORT).show(); 
 
\t \t \t } 
 

 
\t \t \t break; 
 

 
\t \t case R.id.imgView_alphabetImage_2: 
 
\t \t \t if (_imgView_des 
 
\t \t \t \t \t .getDrawable() 
 
\t \t \t \t \t .getConstantState() 
 
\t \t \t \t \t .equals(_imgView_alphabetImage_2.getDrawable() 
 
\t \t \t \t \t \t \t .getConstantState())) { 
 
\t \t \t \t Toast.makeText(this, "matched", Toast.LENGTH_SHORT).show(); 
 
\t \t \t } else { 
 
\t \t \t \t Toast.makeText(this, "not matched", Toast.LENGTH_SHORT).show(); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t break; 
 
\t \t \t 
 
\t \t case R.id.btn_refresh: 
 
\t \t \t 
 
\t \t \t Random random = new Random(); 
 
\t \t \t int index = random.nextInt(7); 
 
\t \t \t _imgView_des.setImageResource(_alphabet_arrayList.get(index)); 
 
\t \t \t _imgView_des.setVisibility(View.VISIBLE); 
 
\t \t \t _tempArrayList = (ArrayList<Integer>) _alphabet_arrayList.clone(); 
 
\t \t \t _tempArrayList.remove(index); 
 
\t \t \t Collections.shuffle(_tempArrayList, random); 
 
\t \t \t for (int j = 0; j < 1; j++) { 
 
\t \t \t \t _finalTempArrayList.add(_tempArrayList.get(j)); 
 
\t \t \t } 
 
\t \t \t _finalTempArrayList.add(_alphabet_arrayList.get(index)); 
 
\t \t \t Collections.shuffle(_finalTempArrayList); 
 
\t \t \t Log.e(" _finalTempArrayList after suffel", _finalTempArrayList.toString()); 
 
\t \t \t _imgView_alphabetImage_1.setImageResource(_finalTempArrayList.get(0)); 
 
\t \t \t _imgView_alphabetImage_2.setImageResource(_finalTempArrayList.get(1)); 
 
\t \t \t _finalTempArrayList.clear(); 
 
\t \t \t break; 
 
\t \t \t 
 
\t \t default: 
 
\t \t \t break; 
 
\t \t } 
 

 
\t } 
 

 
\t @Override 
 
\t public boolean onTouch(View v, MotionEvent e) { 
 
\t \t if (e.getAction() == MotionEvent.ACTION_DOWN) { 
 
\t \t \t // ClipData clipData = ClipData.newPlainText("", ""); 
 
\t \t \t DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); 
 
\t \t \t v.startDrag(null, shadowBuilder, v, 0); 
 
\t \t \t v.setVisibility(View.INVISIBLE); 
 
\t \t \t return true; 
 
\t \t } else { 
 
\t \t \t return false; 
 
\t \t } 
 
\t } 
 

 
\t @Override 
 
\t public boolean onDrag(View v, DragEvent e) { 
 
\t \t switch (e.getAction()) { 
 

 
\t \t case DragEvent.ACTION_DRAG_STARTED: 
 
\t \t \t // if (e.getClipDescription().hasMimeType(
 
\t \t \t // ClipDescription.MIMETYPE_TEXT_PLAIN)) { 
 
\t \t \t // return true; 
 
\t \t \t // } else { 
 
\t \t \t // Toast.makeText(this, "can not accept the image", 
 
\t \t \t // Toast.LENGTH_SHORT).show(); 
 
\t \t \t // 
 
\t \t \t // } 
 

 
\t \t \t // return false; 
 
\t \t \t break; 
 

 
\t \t case DragEvent.ACTION_DROP: 
 
\t \t \t if (_imgView_des 
 
\t \t \t \t \t .getDrawable() 
 
\t \t \t \t \t .getConstantState() 
 
\t \t \t \t \t .equals(_imgView_alphabetImage_1.getDrawable() 
 
\t \t \t \t \t \t \t .getConstantState())) { 
 
\t \t \t \t ViewGroup viewGroup = (ViewGroup) v.getParent(); 
 
\t \t \t \t viewGroup.removeView(_imgView_des); 
 
\t \t \t \t v.setBackground(this.getResources().getDrawable(R.drawable.a)); 
 
\t \t \t \t return true; 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t \t else if(_imgView_des 
 
\t \t \t \t \t .getDrawable() 
 
\t \t \t \t \t .getConstantState() 
 
\t \t \t \t \t .equals(_imgView_alphabetImage_2.getDrawable() 
 
\t \t \t \t \t \t \t .getConstantState())) 
 
\t \t \t { 
 
\t \t \t \t ViewGroup viewGroup = (ViewGroup) v.getParent(); 
 
\t \t \t \t viewGroup.removeView(_imgView_des); 
 
\t \t \t \t v.setBackground(this.getResources().getDrawable(R.drawable.a)); 
 
\t \t \t \t return true; \t 
 
\t \t \t } 
 
// \t \t \t else { 
 
// \t \t \t \t return false; 
 
// \t \t \t } 
 
\t \t 
 
\t \t \t break; 
 
\t \t \t 
 
\t \t \t 
 
\t \t case DragEvent.ACTION_DRAG_ENDED: 
 
\t \t \t Log.v("a", e.getResult() + ""); 
 
\t \t \t if (e.getResult()) { 
 
\t \t \t \t _imgView_des.setVisibility(View.INVISIBLE); 
 
\t \t \t \t Log.v("asddd", e.getResult() + ""); 
 
\t \t \t \t Toast.makeText(this, " accept the image", 
 
\t \t \t \t \t Toast.LENGTH_SHORT).show(); 
 
\t \t \t \t return true; 
 
\t \t \t } else { 
 
\t \t \t \t _imgView_des.setVisibility(View.VISIBLE); 
 
\t \t \t \t Toast toast = new Toast(this); 
 
\t \t \t  ImageView view = new ImageView(this); 
 
\t \t \t  view.setImageResource(R.drawable.unsuccess); 
 
\t \t \t  toast.setView(view); 
 
\t \t \t  toast.show(); 
 
\t \t \t  return true; 
 
\t \t \t } 
 

 
\t \t 
 

 
\t \t default: 
 
\t \t \t break; 
 
\t \t } 
 
\t \t return false; 
 

 
\t } 
 
}

回答

0

我检查你的代码的问题是您的onDrag方法。

大小写DragEvent.ACTION_DROP事件未调用,因为您没有返回标志大小写DragEvent.ACTION_DRAG_STARTED:event。你必须通过真正的标志像下面的代码

case DragEvent.ACTION_DRAG_STARTED: 

return true; 
+0

书面方式真也接受了错误的字母图像后,以及在显示敬酒两次 – 2015-04-06 11:22:32

+0

@Ashutoshsingh - 你只检查的ImageView的绘制。没有计算拖动的imageview的位置。所以如果你通过_imgView_alphabetImage_1拖动图片,那么它将检查两个imageview。所以你需要检查第一个用户在哪个图像视图上拖动图像。 – 2015-04-06 11:29:00

+0

你可以帮我pllzzzz .....怎么做? – 2015-04-07 07:50:30