页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/change_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="88dp" android:gravity="center" android:text="石头剪刀布" android:textColor="@color/colorPrimary" android:textSize="30sp" /> <LinearLayout android:layout_width="422dp" android:layout_height="288dp" android:gravity="center" android:orientation="horizontal"> <ImageView android:id="@+id/play" android:layout_width="180dp" android:layout_height="180dp" android:background="@drawable/you" /> <ImageView android:id="@+id/play2" android:layout_width="180dp" android:layout_height="180dp" android:background="@drawable/me" /> </LinearLayout> <LinearLayout android:layout_width="422dp" android:layout_height="74dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="开始游戏" /> <Button android:id="@+id/button1" android:layout_width="122dp" android:layout_height="wrap_content" android:gravity="center" android:onClick="myClick" android:text="确定" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="开始游戏" /> </LinearLayout> </LinearLayout>
MA的程序 package com.example. import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Vibrator; import android.widget.ImageView; import android.widget.Toast; import java.util.Random; public class MainActivity extends AppCompatActivity { private SensorManager manager; private Sensor sensor; private int what = 0; private Vibrator vibrator; private ImageView play,play2; int[] images = {R.drawable.stone,R.drawable.scissors,R.drawable.cloth}; Random r = new Random(); int you = r.nextInt(3); int me = r.nextInt(3); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (ImageView) findViewById(R.id.play); play2 = (ImageView) findViewById(R.id.play2); manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL); vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); } SensorEventListener listener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { float[] values = event.values; float x = values[0]; float y = values[1]; float z = values[2]; int coll = 18; if (Math.abs(x)>coll|Math.abs(y)>coll|Math.abs(z)>coll) { long[]pattern = {1000}; vibrator.vibrate(pattern,-1); chaiQuan(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; private void chaiQuan() { switch (不知道这里写什么) { case R.id.button: play.setImageResource(images[you]); break; case R.id.button: getA(); break; case R.id.button2: play2.setImageResource(images[me]); break; } } private void getA() { int one = images[0] ; int two = images[1]; int three = images[2]; Intent intent = getIntent(); String play = intent.getStringExtra("play"); String play2 = intent.getStringExtra("play2"); String msg = "www"+play+"lll"+play2+"/n"; if(play.equals(play2)){ Toast.makeText(this,msg+"平局",Toast.LENGTH_SHORT).show(); } if (play2.equals(play)){ Toast.makeText(this,msg+"平局",Toast.LENGTH_SHORT).show(); } if(play.equals(one)) { if (play2.equals(two)) { Toast.makeText(this, msg + "play", Toast.LENGTH_SHORT).show(); } else if (play2.equals(three)) { Toast.makeText(this, msg + "play", Toast.LENGTH_SHORT).show(); } } if (play.equals(two)){ if (play2.equals(three)){ Toast.makeText(this,msg+"play",Toast.LENGTH_SHORT).show(); }else if(play2.equals(one)){ Toast.makeText(this,msg+"play",Toast.LENGTH_SHORT).show(); } } if (play.equals(three)) { if (play2.equals(one)){ Toast.makeText(this,msg+"play",Toast.LENGTH_SHORT).show(); }else if(play2.equals(two)){ Toast.makeText(this,msg+"play",Toast.LENGTH_SHORT).show(); } } } @Override protected void onPause() { super.onPause(); manager.unregisterListener(listener); } }
有什么问题建议再描述