public class MainActivity extends Activity {
private Button but1;
private Button but2;
private Button but3;
private Button but4;
private Button but5;
private Button but6;
private Button but7;
private Button but8;
private Button but9;
private Button start;
private List<Button> bt;
private static int time=0;//控制循环为3次
private static int lastclick=-1;
private static int choosetwo=0;
private AsyTask at;//step 1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1=(Button)findViewById(R.id.but1);
but2=(Button)findViewById(R.id.but2);
but3=(Button)findViewById(R.id.but3);
but4=(Button)findViewById(R.id.but4);
but5=(Button)findViewById(R.id.but5);
but6=(Button)findViewById(R.id.but6);
but7=(Button)findViewById(R.id.but7);
but8=(Button)findViewById(R.id.but8);
but9=(Button)findViewById(R.id.but9);
start=(Button)findViewById(R.id.start);
bt=new ArrayList<Button>();
bt.add(start);
bt.add(but1);
bt.add(but2);
bt.add(but3);
bt.add(but4);
bt.add(but5);
bt.add(but6);
bt.add(but7);
bt.add(but8);
bt.add(but9);
SharedPreferences pref=getSharedPreferences("items",MODE_PRIVATE);
Editor edt=pref.edit();
edt.putString("item1", "目前最大的愿望");
edt.putString("item2", "选择做一个电影中的角色,你选谁");
edt.putString("item3", "你觉得自己放的屁臭不臭");
edt.putString("item4", "有一天我和你吵架,你会怎么办");
edt.putString("item5", "你不开心的时候会有什么表现");
edt.putString("item6", " 你每天都洗屁屁么");
edt.putString("item7", "如果一天我对你说我爱你,你怎么办");
edt.putString("item8", "如果可以,你愿意拥有什么超能力呢");
edt.putString("item9", "做自己最性感、最妩媚的表情或动作");
edt.putString("item10", "一见钟情、三见倾情和日久生情,哪个更是你的风格");
edt.putString("item11", "最近一次发自内心的笑是什么时候");
edt.putString("item12", "做过最尴尬的事是什么");
edt.putString("item13", "在你眼里我是什么样子的人");
edt.putString("item14", "最想收到什么礼物");
edt.putString("item15", "做一个鬼脸,拍照纪念");
edt.putString("item16", "你觉得和男/女朋友进行到哪一步了");
edt.putString("item17", "最喜欢自己五官哪个部位");
edt.putString("item18", "如果明天就是最后一天,你会做什么");
edt.putString("item19", "给你一天做异性会干什么呢");
edt.putString("item20", "曾经有过彻夜未眠吗,什么事");
edt.putString("item21", "说说你觉得哪些地方处事成熟");
edt.commit();
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
at=new AsyTask();//step2
if(start.getText().equals("开始"))
{
start.setText("结束");
at.execute(""); //step3
}
else
{
at.cancel(true);//中途点击结束按钮
start.setText("开始");
System.out.println("调用了取消");
}
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class AsyTask extends AsyncTask<String,Integer,Integer>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
//start.getText().equals("结束")
if(time==0 && lastclick!=-1)//上次点击结束后,再次点击按钮的时候,清空上次点击后的按钮背景。lastclick!=-1表示不是第一次点击,就需要清空
{
bt.get(lastclick).setPressed(false);
bt.get(lastclick).setFocusable(false);
}
//生成随机数选择按钮
Random rd=new Random();
while(true)
{
int choose=rd.nextInt(9)+1;
if(lastclick!=choose)//不重复选取按钮
{
lastclick=choose;
break;
}
}
bt.get(lastclick).setPressed(true);
bt.get(lastclick).setFocusable(true);
}
@Override
protected Integer doInBackground(String... arg0) {
// TODO Auto-generated method stub
//if( start.getText().equals("结束"))
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return lastclick;
}
@Override<pre name="code" class="java"> //step6 如果运行了cancel方法则不运行这个方法
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);
if( start.getText().equals("结束"))
{
if(time!=2)//控制循环3次,前两次将上次选项清空,开启下一个线程
{
bt.get(lastclick).setPressed(false);
bt.get(lastclick).setFocusable(false);
at=new AsyTask();
at.execute("");
time++;
}
else//最后一次出现字幕,不要清空按钮了,等到再次点击按钮时根据time和lastclick判断
{
while(true)
{
Random rd=new Random();
int choose=rd.nextInt(21)+1;
if(choosetwo!=choose)//不重复选取条目
{
choosetwo=choose;
break;
}
}
String str="item"+choosetwo;
SharedPreferences pref=getSharedPreferences("items",MODE_PRIVATE);
String message=pref.getString(str, "");
TextView edt=(TextView)findViewById(R.id.tv1);
edt.setText(message);
start.setText("开始");
time=0;//次数恢复为0
}
}
}
@Override
protected void onCancelled(Integer result) {
// TODO Auto-generated method stub
while(true)
{
Random rd=new Random();
int choose=rd.nextInt(21)+1;
if(choosetwo!=choose)//不重复选取按钮
{
choosetwo=choose;
break;
}
}
String str="item"+choosetwo;
SharedPreferences pref=getSharedPreferences("items",MODE_PRIVATE);
String message=pref.getString(str, "");
TextView edt=(TextView)findViewById(R.id.tv1);
edt.setText(message);
time=0;//次数恢复为0
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/start"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<Button
android:id="@+id/but1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="1" />
<Button
android:id="@+id/but2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="2" />
<Button
android:id="@+id/but3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="3" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/but4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="4" />
<Button
android:id="@+id/but5"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="5" />
<Button
android:id="@+id/but6"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="6" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/but7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="7" />
<Button
android:id="@+id/but8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="8" />
<Button
android:id="@+id/but9"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/select"
android:text="9" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:text="真心话大冒险" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:text="开始 />
</RelativeLayout>
button会使用到selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false"
android:drawable="@drawable/onew"/>
<item android:state_pressed="true"
android:drawable="@drawable/two"/>
<item android:state_focused="true"
android:drawable="@drawable/two"/>
</selector>
还需要添加图片即可!
程序目的:点击开始按钮,随机有序显示3个按钮,最后停止时,显示出现的结果。中途也可以再次点击按钮,就停止在当前按钮。
要点:1.线程有序执行方法:在异步线程的doInBackground中开启新的线程即可
2.sharedPreferences的使用
3.button使用selector自定义背景图片
4.中途点击结束,开始和结束是同一个按钮,到底调用哪个方法是根据按钮上面显示的文字来进行分支判断的
总结:异步线程中除了doInBackground方法不能够更新UI组件,其余方法可以更新
doInBackground进行一些耗时操作,线程休眠等