使用Android阵列中的随机字符串创建对话警报

时间:2012-04-16 19:39:02

标签: android eclipse

我正在尝试为andriod编写一个应用程序(使用eclipse),其中当按下一个图像按钮时,一个alertdialouge会出现一个随机字符串来自和数组,每次按下该按钮我希望它能改变字符串从阵列。我编写了一个alertdialouge和一些代码,它获取一个随机字符串,但它是一个文本视图,而不是一个警报dialouge。你能看看我的代码并告诉我需要改变什么吗?

package kevin.erica.box;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;

public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private static final Random rgenerator = new Random();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources();

    myString = res.getStringArray(R.array.myArray); 

    list = myString[rgenerator.nextInt(myString.length)];

    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(list);
}

public void kevin(View view)
{
new AlertDialog.Builder(this).setTitle("The Box").setMessage(getResources().getText(R.string.list)).setNeutralButton("Close", null).show(); }
}

3 个答案:

答案 0 :(得分:1)

据我所知,只要按下特定的ImageButton,就需要在数组中显示随机选择的文本字符串。

请尝试以下代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources();

   myString = res.getStringArray(R.array.myArray); 

   list = myString[rgenerator.nextInt(myString.length)];

   ImageButton ib = (ImageButton) findViewById(R.id.img_button_id);
   ib.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View arg0) {
           AlertDialog.Builder b = new AlertDialog.Builder(TheKevinAndEricaBoxActivity.this);
           b.setMessage(myString[rgenerator.nextInt(myString.length)]);
           Dialog d = b.create();
           d.show();
       }
   });
}

答案 1 :(得分:0)

{...

  list = myString[rgenerator.nextInt(myString.length)];

  TextView tv = (TextView) findViewById(R.id.textView1);
  tv.setText(list);
}

public void kevin(View view)
{
  new AlertDialog.Builder(this).setTitle("The Box").setMessage(getResources().getText(R.string.list)).setNeutralButton("Close", null).show(); 
}
...}

R.string.list是一个在运行时永远不会改变的静态字符串,它在res / values / strings.xml中定义

我会完全删除TextView并传入要在警报中显示的值。

{...

  String randomInt = myString[rgenerator.nextInt(myString.length)];
  showAlert(randomInt);
}

public void showAlert(String randomInt){
  new AlertDialog.Builder(this).setTitle("The Box").setMessage(
    "Here's a random number: " + randomInt
    ).setNeutralButton("Close", null).show(); 
}

...}

答案 2 :(得分:0)

您只需更改.setTitle()或.setMessage()即可传入您的值。这样的事情应该适合你:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(list)
    .setCancelable(false)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // put your code here
        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // put your code here 
            dialog.cancel();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

如果您不尝试将所有链式方法调用塞入一行,则更容易理解发生了什么。

此外,您似乎得到了这两个向后的变量名称:

private String[] myString;
private String list;