1 package com.example.zs; 2 3 import java.util.*; 4 5 import android.support.v7.app.ActionBarActivity; 6 import android.support.v7.app.ActionBar; 7 import android.support.v4.app.Fragment; 8 import android.os.Bundle; 9 import android.view.LayoutInflater; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.view.ViewGroup; 15 import android.widget.*; 16 import android.widget.AdapterView.OnItemSelectedListener; 17 import android.os.Build; 18 19 public class MainActivity extends ActionBarActivity { 20 21 private Button b1,b2,b3; 22 private LinearLayout l1; 23 private Spinner sp1; 24 private TextView[]T=new TextView[9]; 25 private EditText[]E=new EditText[6]; 26 int i,k,n; 27 long begintime,endtime,time; 28 int[]a=new int[5]; 29 int[]b=new int[5]; 30 int[]c=new int[5]; 31 int[]d=new int[5]; 32 float[]answer=new float[5]; 33 List<String>list=new ArrayList<String>(); 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) 37 { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 41 b1=(Button)findViewById(R.id.button1); 42 b2=(Button)findViewById(R.id.button2); 43 b3=(Button)findViewById(R.id.button3); 44 l1=(LinearLayout)findViewById(R.id.container); 45 T[0]=(TextView)findViewById(R.id.textView4); 46 T[1]=(TextView)findViewById(R.id.textView6); 47 T[2]=(TextView)findViewById(R.id.textView9); 48 T[3]=(TextView)findViewById(R.id.textView12); 49 T[4]=(TextView)findViewById(R.id.textView15); 50 T[5]=(TextView)findViewById(R.id.textView18); 51 T[6]=(TextView)findViewById(R.id.textView20); 52 T[7]=(TextView)findViewById(R.id.textView22); 53 T[8]=(TextView)findViewById(R.id.textView24); 54 E[0]=(EditText)findViewById(R.id.editText2); 55 E[1]=(EditText)findViewById(R.id.editText3); 56 E[2]=(EditText)findViewById(R.id.editText4); 57 E[3]=(EditText)findViewById(R.id.editText5); 58 E[4]=(EditText)findViewById(R.id.editText6); 59 E[5]=(EditText)findViewById(R.id.editText1); 60 sp1=(Spinner)findViewById(R.id.spinner1); 61 list.add("默认"); 62 list.add("勤劳的宿舍长"); 63 list.add("飞奔的宿舍长"); 64 list.add("下海的宿舍长"); 65 list.add("发情期的宿舍长"); 66 list.add("遭袭的宿舍长"); 67 list.add("变身后的宿舍长"); 68 ArrayAdapter<String> adapter; 69 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list); 70 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 71 sp1.setAdapter(adapter); 72 73 b1.setOnClickListener(new OnClickListener() 74 { 75 76 @Override 77 public void onClick(View v) 78 { 79 // TODO Auto-generated method stub 80 try 81 { 82 for(k=0;k<5;k++) 83 { 84 T[k].setText(null); 85 } 86 for(k=0;k<5;k++) 87 { 88 E[k].setText(null); 89 } 90 T[5].setText(null); 91 T[6].setText(null); 92 T[7].setText(null); 93 T[8].setText(null); 94 i=0; 95 if(E[5].getText().length()==0) 96 { 97 Toast.makeText(MainActivity.this, "请输入题数", 1).show(); 98 } 99 else if(Integer.parseInt(E[5].getText()+"")>5 || Integer.parseInt(E[5].getText()+"")<1) 100 { 101 Toast.makeText(MainActivity.this, "题数最多不超过5题,不少于1题", 1).show(); 102 } 103 else 104 { 105 i=Integer.parseInt(E[5].getText()+""); 106 107 for(k=0;k<i;k++) 108 { 109 a[k]=(int)(Math.random()*201-100); 110 b[k]=(int)(Math.random()*201-100); 111 c[k]=(int)(Math.random()*5); 112 d[k]=(int)(Math.random()*12+1); 113 Core core=new Core(a[k],b[k],c[k],d[k]); 114 answer[k]=core.calc(); 115 T[k].setText(core.toString()); 116 117 } 118 begintime = System.currentTimeMillis(); 119 } 120 }catch(NumberFormatException u) 121 { 122 Toast.makeText(MainActivity.this, "请输入正确的数字", 1).show(); 123 } 124 } 125 }); 126 127 b2.setOnClickListener(new OnClickListener() { 128 129 @Override 130 public void onClick(View v) { 131 // TODO Auto-generated method stub 132 if(i==0) 133 { 134 Toast.makeText(MainActivity.this, "请先开始答题", 1).show(); 135 } 136 else 137 { 138 endtime = System.currentTimeMillis(); 139 time=(endtime-begintime)/1000; 140 n=0; 141 for(k=0;k<i;k++) 142 { 143 if(E[k].getText().length()==0) 144 { 145 continue; 146 } 147 try 148 { 149 if(answer[k]==Float.parseFloat(E[k].getText()+"")) 150 { 151 n++; 152 } 153 }catch(NumberFormatException u) 154 { 155 Toast.makeText(MainActivity.this, "请输入正确的数字", 1).show(); 156 } 157 158 } 159 T[5].setText(n+"道题"); 160 T[6].setText((i-n)+"道题"); 161 T[7].setText(time+"秒"); 162 T[8].setText(""); 163 for(k=0;k<i;k++) 164 { 165 T[8].setText(T[8].getText()+""+answer[k]+";"); 166 } 167 i=0; 168 } 169 } 170 }); 171 172 b3.setOnClickListener(new OnClickListener() { 173 174 @Override 175 public void onClick(View v) { 176 // TODO Auto-generated method stub 177 for(k=0;k<5;k++) 178 { 179 T[k].setText(null); 180 } 181 for(k=0;k<5;k++) 182 { 183 E[k].setText(null); 184 } 185 E[5].setText(null); 186 T[5].setText(null); 187 T[6].setText(null); 188 T[7].setText(null); 189 T[8].setText(null); 190 i=0; 191 } 192 }); 193 194 sp1.setOnItemSelectedListener(new OnItemSelectedListener() { 195 @Override 196 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 197 { 198 if(pos==0) 199 l1.setBackgroundResource(R.drawable.ss2); 200 if(pos==1) 201 l1.setBackgroundResource(R.drawable.ss3); 202 if(pos==2) 203 l1.setBackgroundResource(R.drawable.ss4); 204 if(pos==3) 205 l1.setBackgroundResource(R.drawable.ss5); 206 if(pos==4) 207 l1.setBackgroundResource(R.drawable.ss6); 208 if(pos==5) 209 l1.setBackgroundResource(R.drawable.ss7); 210 if(pos==6) 211 l1.setBackgroundResource(R.drawable.ss8); 212 } 213 @Override 214 public void onNothingSelected(AdapterView<?> parent) 215 { 216 // Another interface callback 217 } 218 }); 219 } 220 221 222 @Override 223 public boolean onCreateOptionsMenu(Menu menu) { 224 225 // Inflate the menu; this adds items to the action bar if it is present. 226 getMenuInflater().inflate(R.menu.main, menu); 227 return true; 228 } 229 230 @Override 231 public boolean onOptionsItemSelected(MenuItem item) { 232 // Handle action bar item clicks here. The action bar will 233 // automatically handle clicks on the Home/Up button, so long 234 // as you specify a parent activity in AndroidManifest.xml. 235 int id = item.getItemId(); 236 if (id == R.id.action_settings) { 237 return true; 238 } 239 return super.onOptionsItemSelected(item); 240 } 241 242 /** 243 * A placeholder fragment containing a simple view. 244 */ 245 public static class PlaceholderFragment extends Fragment { 246 247 public PlaceholderFragment() { 248 } 249 250 @Override 251 public View onCreateView(LayoutInflater inflater, ViewGroup container, 252 Bundle savedInstanceState) { 253 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 254 return rootView; 255 } 256 } 257 258 }
相关文章: