2015-04-23 20 views
0

我真的很陌生,有一个项目,涉及在测验中保持高分。如何在游戏结束后在片段上显示阵列列表?显示数组列表作为Android工作室的高分

public void saveHighScores(int score) { 
     ArrayList<String> topFiveHighScoresList; 

     // store the saved scores in an ArrayList 
     topFiveHighScoresList = new ArrayList<String>(topFiveHighScoresPreference.getAll().keySet()); 

     // add the latest score to the list 
     topFiveHighScoresList.add(Integer.toString(score)); 

     // sort the scores 
     Collections.sort(topFiveHighScoresList, String.CASE_INSENSITIVE_ORDER); 
      // remove the last score from the list if the list exceeds 5 items. 
     if (topFiveHighScoresList.size() > 5) 
      topFiveHighScoresList.remove(5); 

     SharedPreferences.Editor preferencesEditor; 
     preferencesEditor = topFiveHighScoresPreference.edit(); 

     preferencesEditor.clear(); 
     preferencesEditor.apply(); 

    } 
+0

“游戏结束后”是什么意思? – Rami

+0

你发现答案有用吗?或者你需要别的东西 – MiguelHincapieC

回答

0

您可以使用自定义对话框(扩展对话框)。

在下一个代码中,您将显示带分数的对话框。

首先XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#dc000000"> 


    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/button_close_text" 
     android:id="@+id/button_close" 
     android:layout_gravity="center_horizontal" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:textColor="@color/white" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/scrollView" 
     android:layout_gravity="center_horizontal" > 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/linear_layout"> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

的XML以上是要显示在顶部的按钮时,元素的列表(键 - 值)在用的LinearLayout ID:linear_layout。

二定制对话框:

import android.app.Dialog; 
import android.content.Context; 
import android.support.v4.app.FragmentActivity; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class CustomDialog extends Dialog { 

    private LinearLayout layout; 
    private Context mContext; 

    public CustomDialog(Context context) { 

     super(context, android.R.style.Theme_Translucent_NoTitleBar); 
     setContentView(R.layout.dialog_content); 
     setCancelable(false); 
     mContext = context; 

     Button buttonClose = (Button)findViewById(R.id.button_close); 
     buttonClose.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       dismiss(); 
      } 
     }); 

     layout = (LinearLayout)findViewById(R.id.linear_layout); 
    } 

    public void addKeyValuePair(String key, String value) { 

     TextView textView_key = (TextView)getLayoutInflater().inflate(R.layout.row_key, layout, false); 
     TextView textView_value = (TextView)getLayoutInflater().inflate(R.layout.row_value, layout, false); 

     textView_key.setText(key); 
     textView_value.setText(value); 

     layout.addView(textView_key); 
     layout.addView(textView_value); 
    } 
} 

随着方法:addKeyValuePair(String键,字符串值),你要演出之前将其得分添加到该对话框。

你仍然需要为每一行的键值的XML:

row_key.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="dummy_text" 
    android:id="@+id/row_textView_key" 
    android:layout_marginTop="20sp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:textSize="30sp" 
    android:layout_marginLeft="20sp"> 
</TextView> 

row_value.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="dummy_value" 
    android:id="@+id/row_textView_value" 
    android:layout_marginTop="5sp" 
    android:layout_marginLeft="20sp" 
    android:textColor="@color/white" 
    android:textSize="30sp"> 
</TextView> 

在你的FragmentActivity,当你想要显示分数:

CustomDialog cd = new CustomDialog(this); 
cd.addKeyValuePair("key","value"); 

就是这样。