我正在尝试进行字母考试。我有一个ImageView11
,它显示了你必须猜测的随机字母。 imageView_alphabet_image_1
和imageView_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,
OnTouchListener, OnDragListener {
private ImageView _imgView_des;
private ImageView _imgView_alphabetImage_1;
private ArrayList<Integer> _alphabet_arrayList;
private Button _btn_refresh;
private ImageView _imgView_alphabetImage_2;
private ArrayList<Integer> _tempArrayList;
private ArrayList<Integer> _finalTempArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
setContentView(R.layout.activity_main);
_imgView_des = (ImageView) findViewById(R.id.imgView_des);
_imgView_des.setOnClickListener(this);
_imgView_des.setOnTouchListener(this);
_imgView_des.setOnDragListener(this);
_imgView_alphabetImage_1 = (ImageView) findViewById(R.id.imgView_alphabetImage_1);
_imgView_alphabetImage_1.setOnClickListener(this);
_imgView_alphabetImage_1.setOnDragListener(this);
_imgView_alphabetImage_2 = (ImageView) findViewById(R.id.imgView_alphabetImage_2);
_imgView_alphabetImage_2.setOnClickListener(this);
_imgView_alphabetImage_2.setOnDragListener(this);
_btn_refresh = (Button) findViewById(R.id.btn_refresh);
_btn_refresh.setOnClickListener(this);
}
private void initView() {
_alphabet_arrayList = new ArrayList<Integer>();
_alphabet_arrayList.add(R.drawable.a);
_alphabet_arrayList.add(R.drawable.b);
_alphabet_arrayList.add(R.drawable.c);
_alphabet_arrayList.add(R.drawable.d);
_alphabet_arrayList.add(R.drawable.e);
_alphabet_arrayList.add(R.drawable.f);
_alphabet_arrayList.add(R.drawable.g);
_tempArrayList = new ArrayList<Integer>();
_finalTempArrayList = new ArrayList<Integer>();
}
@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgView_des:
break;
case R.id.imgView_alphabetImage_1:
if (_imgView_des
.getDrawable()
.getConstantState()
.equals(_imgView_alphabetImage_1.getDrawable()
.getConstantState())) {
Toast.makeText(this, "matched", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "not matched", Toast.LENGTH_SHORT).show();
}
break;
case R.id.imgView_alphabetImage_2:
if (_imgView_des
.getDrawable()
.getConstantState()
.equals(_imgView_alphabetImage_2.getDrawable()
.getConstantState())) {
Toast.makeText(this, "matched", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "not matched", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_refresh:
Random random = new Random();
int index = random.nextInt(7);
_imgView_des.setImageResource(_alphabet_arrayList.get(index));
_imgView_des.setVisibility(View.VISIBLE);
_tempArrayList = (ArrayList<Integer>) _alphabet_arrayList.clone();
_tempArrayList.remove(index);
Collections.shuffle(_tempArrayList, random);
for (int j = 0; j < 1; j++) {
_finalTempArrayList.add(_tempArrayList.get(j));
}
_finalTempArrayList.add(_alphabet_arrayList.get(index));
Collections.shuffle(_finalTempArrayList);
Log.e(" _finalTempArrayList after suffel", _finalTempArrayList.toString());
_imgView_alphabetImage_1.setImageResource(_finalTempArrayList.get(0));
_imgView_alphabetImage_2.setImageResource(_finalTempArrayList.get(1));
_finalTempArrayList.clear();
break;
default:
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// ClipData clipData = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
switch (e.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// if (e.getClipDescription().hasMimeType(
// ClipDescription.MIMETYPE_TEXT_PLAIN)) {
// return true;
// } else {
// Toast.makeText(this, "can not accept the image",
// Toast.LENGTH_SHORT).show();
//
// }
// return false;
break;
case DragEvent.ACTION_DROP:
if (_imgView_des
.getDrawable()
.getConstantState()
.equals(_imgView_alphabetImage_1.getDrawable()
.getConstantState())) {
ViewGroup viewGroup = (ViewGroup) v.getParent();
viewGroup.removeView(_imgView_des);
v.setBackground(this.getResources().getDrawable(R.drawable.a));
return true;
}
else if(_imgView_des
.getDrawable()
.getConstantState()
.equals(_imgView_alphabetImage_2.getDrawable()
.getConstantState()))
{
ViewGroup viewGroup = (ViewGroup) v.getParent();
viewGroup.removeView(_imgView_des);
v.setBackground(this.getResources().getDrawable(R.drawable.a));
return true;
}
// else {
// return false;
// }
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.v("a", e.getResult() + "");
if (e.getResult()) {
_imgView_des.setVisibility(View.INVISIBLE);
Log.v("asddd", e.getResult() + "");
Toast.makeText(this, " accept the image",
Toast.LENGTH_SHORT).show();
return true;
} else {
_imgView_des.setVisibility(View.VISIBLE);
Toast toast = new Toast(this);
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.unsuccess);
toast.setView(view);
toast.show();
return true;
}
default:
break;
}
return false;
}
}
答案 0 :(得分:0)
我检查了您的 onDrag 方法中的问题代码。
case DragEvent.ACTION_DROP事件没有调用,因为你没有返回标志情况DragEvent.ACTION_DRAG_STARTED:event。你必须传递真正的旗帜,如下面的代码
case DragEvent.ACTION_DRAG_STARTED:
return true;