androidstudioで開発をしています。
1~16の4×4マスでパ○ドラのようにあるマス目をを押してドラッグアンドドロップすることでマス目が入れ替わる仕組みを作っています。
自分でここまでやってみましたが合ってるいるかも全く自信無いし、ここからどうやればいいのかも全くわからず困っている状況です、、
java初心者なのでわからないことだらけなので初歩的な質問かもしれませんがいろいろと教えてください。よろしくお願いします。
package com.example.admin.a1219myapplication;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.admin.a1219myapplication.R.id.button1;
import static com.example.admin.a1219myapplication.R.id.text;
/**
- Created by admin on 2016/12/22.
*/
public class Certification extends AppCompatActivity{
//ハンドルをタップするとtrue,指が画面から離れるとfalse private boolean mSortable = false; //ドラッグ位置 private int mtext = -1; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //xmlのボタンと対応付け Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2); Button button3 = (Button) findViewById(R.id.button3); Button button4 = (Button) findViewById(R.id.button4); Button button5 = (Button) findViewById(R.id.button5); Button button6 = (Button) findViewById(R.id.button6); Button button7 = (Button) findViewById(R.id.button7); Button button8 = (Button) findViewById(R.id.button8); Button button9 = (Button) findViewById(R.id.button9); Button button10 = (Button) findViewById(R.id.button10); Button button11 = (Button) findViewById(R.id.button11); Button button12 = (Button) findViewById(R.id.button12); Button button13 = (Button) findViewById(R.id.button13); Button button14 = (Button) findViewById(R.id.button14); Button button15 = (Button) findViewById(R.id.button15); Button button16 = (Button) findViewById(R.id.button16); //ここまで //ボタンに配列,setOnTouchListener,数字をセット Button btn[] = new Button[16]; for(int i=0;i<16;i++){ btn[i] = new Button(this); btn[i].setOnTouchListener(listener); btn[i].setText(i); } } private void swapPanel(int i){ } int i; View.OnTouchListener listener= new View.OnTouchListener(){ @Override public boolean onTouch(View view, MotionEvent event) { TextView text = (TextView)findViewById(R.id.view); Button button = (Button) view; if(event.getAction()==MotionEvent.ACTION_DOWN){ } if (!mSortable) { return false; } switch (event.getAction()) { //タッチ case MotionEvent.ACTION_DOWN: { //押したボタンの数字を取得 String str = text.getText().toString(); text.setText(Integer.parseInt(str)); break; } //押しながら移動 case MotionEvent.ACTION_MOVE: { //リストの何番目をタップしているのかを容易に取得できる // 現在のポジションを取得し //指がリストをまたいだ瞬間に入れ替え // 移動が検出されたら入れ替え if (text != text) { mtext = text; } return true; } //指が離れたら case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_OUTSIDE: { stopDrag(); return true; } } return false; } }; public void stopDrag() { mtext = -1; mSortable = false; }
}
あなたの回答
tips
プレビュー