我有一个可点击的TextView,当你按下它时,它从一个数组列表中检索元素并在同一个TextView上随机显示它们。每次按下TextView,它都会显示另一个元素。
现在,我想用显示的顺序保存这些元素,然后按下前一个按钮,每次显示已被显示的元素的前一个元素,直到第一个元素。< / p>
我自己尝试,但它只保留一个元素而不是所有显示的元素,并且不会向后显示它们。
ArrayList<String> random = new ArrayList<String>();
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
}
String q = myString.remove(0);
Text_View.setText(q);
for (int i=0;i<myString.size();i++) {
random.add(q);
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int counter = myString.size() - 1; counter >= 0; counter--) {
Text_View.setText(random.get(counter));
}
}
});
谢谢..
的更新
我发布了你指出的修正代码,但仍然不起作用,但更糟糕的是,它崩溃了!
公共类MyActivity扩展了Activity {
public int counter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.family_excuses);
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int counter = myString.size() - 1;
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
}
String q = myString.remove(0);
Text_View.setText(q);
for (int i=0;i<myString.size();i++) {
random.add(q);
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Text_View.setText(random.get(counter));
counter--;
}
});
更新2
我做了新的改变。即使我按了一次以上按钮,该按钮仍可正常工作,但仍然,每次只有第一个显示的元素。新代码是:
public class MyActivity extends Activity {
public int counter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.family_excuses);
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
}
String q = myString.remove(0);
Text_View.setText(q);
for (int i=0;i<myString.size();i++) {
random.add(q);
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int counter = myString.size() - 2;
if (counter>=0){
Text_View.setText(random.get(counter));
counter--;
}
抱歉,我不知道如何使用控制台..
答案 0 :(得分:1)
将第二次更新更改为此
public class MyActivity extends Activity {
public int counter;
private static final String TAG = "whatever";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.family_excuses);
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
counter = myString.size() - 2;
}
String q = myString.remove(0);
Text_View.setText(q);
for (int i=0;i<myString.size();i++) {
random.add(q);
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter>=0){
Log.i(TAG,Integer.toString(counter));
Text_View.setText(random.get(counter));
counter--;
}
将您的手机连接到您的计算机,打开Android显示器,运行应用程序并搜索(它有一个搜索栏)您的标签(此处称为无论如何)。它会过滤结果,所以在这里发布它们。还要告诉我Text_array中有多少项,你点击text_View多少次以及prev多少次。例如,当你先按textview 3(或更多)时会发生什么?
我要睡觉了,早上见到你
编辑1:这应该有效,我创建了一个项目来测试它,它适用于我
public class MyActivity extends Activity {
public int counter;
private static final String TAG = "whatever";
ArrayList <String> myString=null;
private TextView Text_View;
private Button prev;
public ArrayList<String> random=new ArrayList<String>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.family_excuses);
Text_View = (TextView) findViewById(R.id.Text_View);
prev = (Button) findViewById(R.id.button);//request location
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
}
String q = myString.remove(0);
Text_View.setText(q);
random.add(q);
counter=random.size()-2;
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter >= 0) {
Text_View.setText(random.get(counter));
counter--;
}
}
});
}
}
唯一的问题应该是,如果你按textview&gt; prev&gt; textview它不起作用,但我们也会修复它,我只是想确保它也适合你
编辑2:这应该是最终版本,这适用于textview&gt; prev&gt; textview。 我不确定该行应该是mark = counter + 2;,随意更改号码,因为我仍然对你要做的事情感到困惑
public class MyActivity extends Activity {
public int counter;
private static final String TAG = "whatever";
ArrayList <String> myString=null;
private TextView Text_View;
private Button prev;
private int mark=-1;
public ArrayList<String> random=new ArrayList<String>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.family_excuses);
Text_View = (TextView) findViewById(R.id.Text_View);
prev = (Button) findViewById(R.id.button);//request location
Text_View.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
if (myString==null||myString.size()==0){
myString = new ArrayList<String>();
Collections.addAll(myString,res.getStringArray(R.array.Text_array));
Collections.shuffle(myString);
}
String q = myString.remove(0);
Text_View.setText(q);
Log.i(TAG,Integer.toString(mark));
if (mark==-1){
random.add(q);
}else {
random.add(mark,q);
}
counter=random.size()-2;
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter >= 0) {
Text_View.setText(random.get(counter));
counter--;
mark=counter+2;
}
}
});
}
}
答案 1 :(得分:0)
我认为你应该改变Text_View.setText(q); 到Text_View.append(q); 编辑:改为:
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Text_View.setText(random.get(counter));
counter--;
}
});
在Text_View.setOnClickListener
中为计数器赋值counter = myString.size() - 1;
并使用全局变量对其进行初始化,例如
public class yourActivity extends AppCompatActivity {
public int counter;
编辑2为您的原始答案添加延迟
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
private Handler mHandler = new Handler();
for (int counter = myString.size() - 1; counter >= 0; counter--) {
mHandler.postDelayed(new Runnable() {
public void run() {
doStuff(random.get(i));
}
}, 5000);
}
}
});
private void doStuff(String i) {
Text_View.setText(i);
}
并将处理程序添加到导入的库