通过创建一个随机字符串从数组的Andr​​oid对话警报 [英] Creating a dialogue alert using a random string from a array Android

查看:78
本文介绍了通过创建一个随机字符串从数组的Andr​​oid对话警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想codeA应用程序的Andr​​iod(使用eclipse),其中当的ImageButton为presses一个alertdialouge出现从一个随机字符串和数组,每个按钮是pressed时间我想它改变从数组中的字符串。我有$ C $光盘alertdialouge有的code它得到一个随机字符串,但它确实为文本视图,而不是警报的台词。能否请您看看我的code和告诉我,我需要改变吗?

 包kevin.erica.box;进口android.app.Activity;
进口android.app.AlertDialog;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.TextView;
进口了java.util.Random;公共类TheKevinAndEricaBoxActivity延伸活动{
/ **当第一次创建活动调用。 * /
私有String [] myString的;
私人字符串列表;
私有静态最终随机rgenerator =新的随机();@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    资源解析度= getResources();    myString的= res.getStringArray(R.array.myArray);    列表=的myString [rgenerator.nextInt(myString.length)];    TextView的电视=(的TextView)findViewById(R.id.textView1);
    tv.setText(名单);
}公共无效凯文(查看视图)
{
新AlertDialog.Builder(本).setTitle(箱子)setMessage(getResources()的getText(R.string.list)。)setNeutralButton(关闭,NULL).show(); }
}


解决方案

据我了解,你需要从当特定的ImageButton是pressed阵列显示随机选择的文本字符串。

请尝试以下code:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    资源解析度= getResources();   myString的= res.getStringArray(R.array.myArray);   列表=的myString [rgenerator.nextInt(myString.length)];   的ImageButton IB =(的ImageButton)findViewById(R.id.img_button_id);
   ib.setOnClickListener(新OnClickListener(){
       @覆盖
       公共无效的onClick(查看为arg0){
           AlertDialog.Builder B =新AlertDialog.Builder(TheKevinAndEricaBoxActivity.this);
           b.setMessage(myString的[rgenerator.nextInt(myString.length)]);
           对话D = b.create();
           d.show();
       }
   });
}

I'm trying to code a application for andriod (using eclipse) in which when a imagebutton is presses a alertdialouge comes up with a random string from and array, each time the button is pressed i would like it to change the string from the array. I have coded a alertdialouge and some code which gets a random string but it does it to a text-view instead of a alert dialouge. Can you please have a look at my code and tell me what i need to change?

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(); }
}

解决方案

As I understand, you need to display a randomly selected text string from an array whenever a particular ImageButton is pressed.

Try the following code:

@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();
       }
   });
}

这篇关于通过创建一个随机字符串从数组的Andr​​oid对话警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆