
<TextView
android:id="@+id/shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shop_name"
android:layout_centerInParent="true"
android:textSize="18sp" />
<Button android:id="@+id/random_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/random_btn_text"/>

<string name="shop_name">店名</string> <string name="random_btn_text">点我</string>
private void init() {
//定义一个数组用来存放我们要的几个店名
shop = new String[]{
"食莆记","谷之味","王大妈水饺","兰州拉面","惠兴饭店"
};
//初始化控件textview
shop_name = (TextView) findViewById(R.id.shop_name);
//初始化控件button
random_btn = (Button) findViewById(R.id.random_btn);
//注册按钮点击监听
random_btn.setOnClickListener(new RandomBtnClick());
}
class RandomBtnClick implements OnClickListener{
@Override
public void onClick(View v) {
//随机数,区间以之前定义的店家的数组为长度
Random random = new Random();
int num = random.nextInt(shop.length);
//textview显示以该随机数为下标对应的商家数组的名字
shop_name.setText(shop[num]);
}
}

原文:http://www.cnblogs.com/superdo/p/4989827.html