我对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 :(得分: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 key,String value),您将在显示之前将分数添加到对话框中。
您仍然需要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");
就是这样。