Advertisement
Guest User

joseph

a guest
Sep 29th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. package com.android;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.animation.Animation;
  9. import android.view.animation.AnimationUtils;
  10. import android.widget.Button;
  11.  
  12.  
  13. public class Game extends Activity {
  14. private Button buttons[]=new Button[9];
  15. private int data[]={1,3,1,4,3,2,4,2};
  16. private int pressed1,pressed2,press=0,index1,index2;
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.game);
  22.  
  23. buttons[0] = (Button)findViewById(R.id.button1);
  24. buttons[1] = (Button)findViewById(R.id.button2);
  25. buttons[2] = (Button)findViewById(R.id.button3);
  26. buttons[3] = (Button)findViewById(R.id.button4);
  27. buttons[4] = (Button)findViewById(R.id.button5);
  28. buttons[5] = (Button)findViewById(R.id.button6);
  29. buttons[6] = (Button)findViewById(R.id.button7);
  30. buttons[7] = (Button)findViewById(R.id.button8);
  31.  
  32.  
  33. buttons[0].setOnClickListener(new View.OnClickListener() {
  34.  
  35. public void onClick(View v) {
  36. pressMe(v);
  37. buttons[0].setText(Integer.toString(data[whatIsPressed(v)]));
  38. // TODO Auto-generated method stub
  39.  
  40. }
  41.  
  42. });
  43. buttons[1].setOnClickListener(new View.OnClickListener() {
  44.  
  45. public void onClick(View v) {
  46. pressMe(v);
  47. buttons[1].setText(Integer.toString(data[whatIsPressed(v)]));
  48. // TODO Auto-generated method stub
  49.  
  50. }
  51.  
  52.  
  53. });
  54. buttons[2].setOnClickListener(new View.OnClickListener() {
  55.  
  56. public void onClick(View v) {
  57. pressMe(v);
  58. buttons[2].setText(Integer.toString(data[whatIsPressed(v)]));
  59. // TODO Auto-generated method stub
  60.  
  61. }
  62.  
  63.  
  64. });
  65. buttons[3].setOnClickListener(new View.OnClickListener() {
  66.  
  67. public void onClick(View v) {
  68. pressMe(v);
  69. buttons[3].setText(Integer.toString(data[whatIsPressed(v)]));
  70. // TODO Auto-generated method stub
  71.  
  72. }
  73.  
  74.  
  75. });
  76. buttons[4].setOnClickListener(new View.OnClickListener() {
  77.  
  78. public void onClick(View v) {
  79. pressMe(v);
  80. buttons[4].setText(Integer.toString(data[whatIsPressed(v)]));
  81. // TODO Auto-generated method stub
  82.  
  83. }
  84.  
  85. });
  86. buttons[5].setOnClickListener(new View.OnClickListener() {
  87.  
  88. public void onClick(View v) {
  89. pressMe(v);
  90. buttons[5].setText(Integer.toString(data[whatIsPressed(v)]));
  91. // TODO Auto-generated method stub
  92.  
  93. }
  94.  
  95.  
  96. });
  97. buttons[6].setOnClickListener(new View.OnClickListener() {
  98.  
  99. public void onClick(View v) {
  100. pressMe(v);
  101. buttons[6].setText(Integer.toString(data[whatIsPressed(v)]));
  102. // TODO Auto-generated method stub
  103.  
  104. }
  105.  
  106.  
  107. });
  108. buttons[7].setOnClickListener(new View.OnClickListener() {
  109.  
  110. public void onClick(View v) {
  111. pressMe(v);
  112. buttons[7].setText(Integer.toString(data[whatIsPressed(v)]));
  113. // TODO Auto-generated method stub
  114.  
  115. }
  116.  
  117. });
  118. }
  119. private void compareNumbers (){
  120.  
  121. Animation fadeAnim = AnimationUtils.loadAnimation(this, R.anim.animation);
  122. if(pressed1 == pressed2){
  123. buttons[index1].startAnimation(fadeAnim);
  124. buttons[index2].startAnimation(fadeAnim);
  125. buttons[index1].setVisibility(View.INVISIBLE);
  126. buttons[index2].setVisibility(View.INVISIBLE);
  127. }
  128. }
  129.  
  130. private void pressMe(View v){
  131. if(press == 0){
  132. index1 = whatIsPressed(v);
  133. pressed1 = data[index1];
  134. press = 1;
  135.  
  136. clearAll();
  137. }
  138. else if (press ==1){
  139. index2 = whatIsPressed(v);
  140. pressed2 = data[index2];
  141. compareNumbers();
  142. press = 0;
  143.  
  144. }
  145. }
  146. private int whatIsPressed(View v) {
  147. int index = 0;
  148. for(int i=0; i<buttons.length;i++){
  149. if(buttons[i].getId()==((Button)v).getId()){
  150. index =i;
  151. break;
  152. }
  153. }
  154. return index;
  155. }
  156. private void clearAll(){
  157. for(int x=0;x<buttons.length-1;x++){
  158. buttons[x].setText("");
  159.  
  160. }
  161.  
  162. }
  163.  
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. package com.android;
  172.  
  173. import android.app.Activity;
  174. import android.content.Intent;
  175. import android.os.Bundle;
  176. import android.view.View;
  177. import android.view.View.OnClickListener;
  178. import android.widget.Button;
  179.  
  180. public class Menu extends Activity {
  181. private Button Play;
  182. private Button Highscore;
  183.  
  184. @Override
  185. public void onCreate(Bundle savedInstanceState) {
  186. super.onCreate(savedInstanceState);
  187. setContentView(R.layout.Menu);
  188. Play = (Button)findViewById(R.id.Play);
  189. Highscore = (Button)findViewById(R.id.Highscore);
  190. Play.setOnClickListener(new OnClickListener(){
  191. public void onClick(View view){
  192. Intent i = new Intent(getApplicationContext(), Game.class);
  193. startActivity(i);}
  194. });
  195. Highscore.setOnClickListener(new OnClickListener(){
  196. public void onClick(View view){
  197. Intent i = new Intent(getApplicationContext(), Highscore.class);
  198. startActivity(i);}
  199. });
  200.  
  201.  
  202. }
  203. }
  204.  
  205.  
  206.  
  207. package com.android;
  208.  
  209. public class PeopleUsers {
  210. private String username,password;
  211.  
  212. public PeopleUsers(){
  213.  
  214. }
  215.  
  216. public String getUsername() {
  217. return username;
  218. }
  219.  
  220. public void setUsername(String username) {
  221. this.username = username;
  222. }
  223.  
  224. public String getPassword() {
  225. return password;
  226. }
  227.  
  228. public void setPassword(String password) {
  229. this.password = password;
  230. }
  231.  
  232. public PeopleUsers(String username, String password){
  233. this.username = username;
  234. this.password = password;
  235. }
  236. }
  237.  
  238.  
  239. package com.android;
  240.  
  241. import android.app.Activity;
  242. import android.content.Intent;
  243. import android.os.Bundle;
  244. import android.view.View.OnClickListener;
  245. import android.widget.Button;
  246. import android.widget.EditText;
  247. import android.view.View;
  248.  
  249. public class Registration extends Activity {
  250. /** Called when the activity is first created. */
  251. private EditText Username;
  252. private EditText Password;
  253. private EditText Name;
  254. private Button Registration;
  255.  
  256. @Override
  257. public void onCreate(Bundle savedInstanceState) {
  258. super.onCreate(savedInstanceState);
  259. setContentView(R.layout.registration);
  260. Username = (EditText)findViewById(R.id.Username);
  261. Password = (EditText)findViewById(R.id.Password);
  262. Name = (EditText)findViewById(R.id.Name);
  263. Registration = (Button)findViewById(R.id.Registration);
  264. Registration.setOnClickListener(new OnClickListener(){
  265. public void onClick(View view){
  266. Intent i = new Intent(getApplicationContext(), Activities.class);
  267. startActivity(i);}
  268. });
  269. }}
  270.  
  271.  
  272. package com.android;
  273.  
  274. import android.app.Activity;
  275. import android.content.Intent;
  276. import android.os.Bundle;
  277. import android.view.View.OnClickListener;
  278. import android.widget.Button;
  279. import android.widget.EditText;
  280. import android.view.View;
  281.  
  282. public class Activities extends Activity {
  283. /** Called when the activity is first created. */
  284. private EditText Username;
  285. private EditText Password;
  286. private Button Login;
  287. private Button Register;
  288.  
  289. @Override
  290. public void onCreate(Bundle savedInstanceState) {
  291. super.onCreate(savedInstanceState);
  292. setContentView(R.layout.main);
  293. Username = (EditText)findViewById(R.id.Uname);
  294. Password = (EditText)findViewById(R.id.Pwrd);
  295. Login = (Button)findViewById(R.id.Login);
  296. Register = (Button)findViewById(R.id.Register);
  297. Login.setOnClickListener(new OnClickListener(){
  298. public void onClick(View view){
  299. Intent i = new Intent(getApplicationContext(), Main_menu.class);
  300. startActivity(i);}
  301. });
  302. Register.setOnClickListener(new OnClickListener(){
  303. public void onClick(View view){
  304. Intent i = new Intent(getApplicationContext(), Register.class);
  305. startActivity(i);}
  306. });
  307. }
  308. }
  309.  
  310.  
  311. package com.android;
  312.  
  313. import android.content.ContentValues;
  314. import android.content.Context;
  315. import android.database.Cursor;
  316. import android.database.sqlite.SQLiteDatabase;
  317. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  318. import android.database.sqlite.SQLiteOpenHelper;
  319. public class DBHandler extends SQLiteOpenHelper{
  320. private static String database_name="Users";
  321. private static int version =1;
  322. public DBHandler(Context context, String name, CursorFactory factory,
  323. int version) {
  324. super(context, name, factory, version);
  325. // TODO Auto-generated constructor stub
  326. }
  327. public DBHandler(Context context){
  328. super(context,database_name,null,version);
  329. }
  330. @Override
  331. public void onCreate(SQLiteDatabase db) {
  332. // TODO Auto-generated method stub
  333. String sql="create table user(username varchar(15), password varchar(15), primary key(username))";
  334. db.execSQL(sql);
  335. }
  336.  
  337. @Override
  338. public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
  339. // TODO Auto-generated method stub
  340. db.execSQL("drop table if exist user");
  341. this.onCreate(db);
  342. }
  343.  
  344. public void add(PeopleUsers us){
  345. SQLiteDatabase db = this.getWritableDatabase();
  346. ContentValues values = new ContentValues();
  347. values.put("username", us.getUsername());
  348. values.put("password",us.getPassword());
  349. db.insert("user", null, values);
  350. db.close();
  351. }
  352.  
  353. public PeopleUsers search(String uname){
  354. PeopleUsers u=null;
  355. String where = "username ='"+uname+"'";
  356. SQLiteDatabase db = this.getReadableDatabase();
  357. Cursor cursor =db.query("user", null, where, null, null, null, null);
  358. if(cursor.moveToNext()){
  359. u = new PeopleUsers();
  360. u.setUsername(cursor.getString(0));
  361. u.setPassword(cursor.getString(1));
  362. }
  363. db.close();
  364. return u;
  365. }
  366.  
  367. public void PeopleUsers(PeopleUsers s){
  368. SQLiteDatabase db = this.getWritableDatabase();
  369. String where = "username ='"+s.getUsername()+"'";
  370. ContentValues values = new ContentValues();
  371. values.put("username", s.getUsername());
  372. values.put("password",s.getPassword());
  373. db.update("user", values, where, null);
  374. db.close();
  375. }
  376.  
  377. public void delete(String user){
  378. SQLiteDatabase db = this.getWritableDatabase();
  379. String where = "username ='"+user+"'";
  380. db.delete("user", where, null);
  381. db.close();
  382. }
  383. ;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement