Моя программа - случайная строка
Сначала я нажимаю кнопку для запуска, а затем он будет случайным
Во-вторых, я снова нажимаю ту же кнопку, чтобы остановить, как я могу это сделать?
мой код
package com.Randomsentence;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Randomsentence extends Activity {
protected static final int CONTINUE = 0;
protected static final int STOP = 0;
TextView txt;
int time = 30;
int random;
public String[] myString;
Button bt1;
boolean check = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.txt);
bt1 = (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
thread.start();
}
});
}
// our handler
Handler handler = new Handler() {
public void handleMessage(Message msg) {//display each item in a single line
{
if (check == false) {
Random rgenerator = new Random();
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
txt.setText(q);
}
}
}
};
Thread thread = new Thread() {
@Override
public void run() {
try {
while(true) {
sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
это XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginBottom="40px" android:layout_marginLeft="40px" android:layout_marginRight="40px" android:layout_marginTop="40px">
<TextView android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:text="TextView"
android:id="@+id/txt" android:layout_width="fill_parent"
android:layout_marginRight="40px"
android:layout_marginTop="50px"></TextView>
<Button android:text="Button" android:layout_height="wrap_content"
android:id="@+id/bt1" android:layout_width="wrap_content"
android:layout_below="@+id/txt" android:layout_alignLeft="@+id/txt"
android:layout_alignRight="@+id/txt" android:layout_marginLeft="40px"
android:layout_marginRight="40px" android:layout_marginTop="40px"></Button>
</RelativeLayout>
</LinearLayout>
Это вставка array.xml в значения папки
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="myArray">
<item>ข้าว</item>
<item>ข้าว</item>
<item>เส้นใหญ่</item>
<item>เส้นเล็ก</item>
<item>วุ้นเส้น</item>
<item>ข้าวผัด</item>
<item>เส้นใหญ่</item>
<item>ข้าว</item>
<item>มักกะโรนี</item>
<item>ผัดผัก</item>
<item>ต้มยำ</item>
<item>ทอดกระเทียม</item>
<item>ผัดพริกเผา</item>
<item>ผัดกระเพรา</item>
<item>ผัดน้ำมันหอย</item>
<item>ต้มยำแห้ง</item>
<item>ทอดกระเทียม</item>
<item>แพนง</item>
<item>ผัดกระเพรา</item>
</string-array>
</resources>