diff --git a/soft1614080902221/app/src/main/AndroidManifest.xml b/soft1614080902221/app/src/main/AndroidManifest.xml index 33e74612c..379161e7c 100644 --- a/soft1614080902221/app/src/main/AndroidManifest.xml +++ b/soft1614080902221/app/src/main/AndroidManifest.xml @@ -2,6 +2,13 @@ + + + + + + + - + + + + \ No newline at end of file diff --git a/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity3.java b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity3.java index c638b9983..00f6b6df2 100644 --- a/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity3.java +++ b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity3.java @@ -57,10 +57,11 @@ protected void onCreate(Bundle savedInstanceState) { initButton(); //实现中间1-9个Button功能 timer = (TextView) findViewById(R.id.timer); //获取计时控件 handler.postDelayed(runnable, 1000); //实现右上角计时功能 - rankList(); //实现排行榜Button功能 - + rankList(); //实现本地排行榜Button功能 + networkListButton(); } + //显示当前玩家信息 private void initCurrentPlayerTextView() { Intent recIntent = getIntent();//获取Activity2传递过来的玩家信息 @@ -134,6 +135,17 @@ public void onClick(View v) { } + private void networkListButton() { + Button networkListButton = (Button)findViewById(R.id.NetworkListButton); + networkListButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent jumpIntent = new Intent(Activity3.this, Activity5.class); + startActivity(jumpIntent); + } + }); + } + //强制玩家必须按1-9的数字顺序点击,否则不能消去 class ButtonMonitor implements View.OnClickListener { @Override diff --git a/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity5.java b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity5.java new file mode 100644 index 000000000..e89e57d91 --- /dev/null +++ b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/Activity5.java @@ -0,0 +1,83 @@ +package edu.hzuapps.androidlabs.soft1614080902221; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.widget.Toast; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import okhttp3.Call; +import okhttp3.Response; + +public class Activity5 extends AppCompatActivity { + + private List playerDataList = new ArrayList(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_5); + String address = "https://raw.githubusercontent.com/yangyangyang2017/android-labs-2018/master/soft1614080902221/网络排行榜.json"; + //发送请求 + HttpUtil.sendOkHttpRequest(address, new okhttp3.Callback() { + @Override + public void onFailure(Call call, IOException e) { + Log.d("排行榜","网络异常"); + } + + @Override + public void onResponse(Call call, Response response) throws IOException { + //获取的json数据 + String responseData = response.body().string(); + try { + //解析json + JSONArray jsonArray = new JSONArray(responseData); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + String strPlayerName = jsonObject.getString("playerName"); + String strSex = jsonObject.getString("sex"); + String strGameCharacters = jsonObject.getString("gameCharacters"); + int intTime = jsonObject.getInt("time"); + PlayerData playerData = new PlayerData(strPlayerName, strSex, strGameCharacters, intTime); + playerDataList.add(playerData); + + } + + + } catch (JSONException e) { + e.printStackTrace(); + } + + //更新UI + runOnUiThread(new Runnable() { + @Override + public void run() { + //通过id找到RecyclerView2组件对象 + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view2); + //创建一个LinearLayoutManager对象 + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Activity5.this); + //设置recyclerView的布局方式为LinearLayout + recyclerView.setLayoutManager(linearLayoutManager); + //创建适配器,并将playerDataList传入PlayerDataAdapter的构造函数中 + PlayerDataAdapter playerDataAdapter = new PlayerDataAdapter(playerDataList); + //给recyclerView设置适配器 + recyclerView.setAdapter(playerDataAdapter); + } + }); + + + } + }); + + + } +} diff --git a/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/HttpUtil.java b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/HttpUtil.java new file mode 100644 index 000000000..d50f01b3c --- /dev/null +++ b/soft1614080902221/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902221/HttpUtil.java @@ -0,0 +1,17 @@ +package edu.hzuapps.androidlabs.soft1614080902221; + +import okhttp3.OkHttpClient; +import okhttp3.Request; + +public class HttpUtil{ + + public static void sendOkHttpRequest(String address,okhttp3.Callback callback){ + + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(address) + .build(); + client.newCall(request).enqueue(callback); + } + +} \ No newline at end of file diff --git a/soft1614080902221/app/src/main/res/layout/activity_3.xml b/soft1614080902221/app/src/main/res/layout/activity_3.xml index 3c4bfba5e..b129346bc 100644 --- a/soft1614080902221/app/src/main/res/layout/activity_3.xml +++ b/soft1614080902221/app/src/main/res/layout/activity_3.xml @@ -107,13 +107,13 @@ android:id="@+id/instructionsText" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="166dp" + android:layout_marginBottom="19dp" android:layout_marginLeft="30dp" android:layout_marginStart="30dp" android:text="请按数字顺序消除中间的小方块,\n以右上角时间作为成绩!\n抓紧时间了,加油!" android:textColor="#FF0000" android:textSize="20dp" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/rankListButton" app:layout_constraintStart_toStartOf="parent" /> @@ -121,13 +121,24 @@ android:id="@+id/rankListButton" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="37dp" - android:text="排行榜" + android:layout_marginBottom="11dp" + android:text="本地排行榜" + android:textColor="#0000FF" + android:textSize="30dp" + app:layout_constraintBottom_toTopOf="@+id/NetworkListButton" + app:layout_constraintStart_toStartOf="@+id/NetworkListButton" /> + +