Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android - ต้องการให้กดปุ่มแล้ว Random ครั้งเดียวทำไงครับ กดปุ่มครั้งต่อไปให้มันไม่ต้องRandomอีก



 

Android - ต้องการให้กดปุ่มแล้ว Random ครั้งเดียวทำไงครับ กดปุ่มครั้งต่อไปให้มันไม่ต้องRandomอีก

 



Topic : 113285



โพสกระทู้ ( 35 )
บทความ ( 0 )



สถานะออฟไลน์




Code (Android-Java)
package com.example.logic1;

import java.util.Random;

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final TextView head = (TextView) findViewById(R.id.TextView1);
		final TextView in1 = (TextView) findViewById(R.id.Edit1);
		final TextView in2 = (TextView) findViewById(R.id.Edit2);
		final TextView in3 = (TextView) findViewById(R.id.Edit3);
		final TextView in4 = (TextView) findViewById(R.id.Edit4);
		final TextView out1 = (TextView) findViewById(R.id.Text1);
		final Button but1 = (Button) findViewById(R.id.but1);

		
		but1.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				/*
				Random random = new Random();
				int[] s = new int[4];
				s[0] = random.nextInt(10);
				s[1] = random.nextInt(10);
				while (s[1] == s[0])
				{
					s[1] = random.nextInt(10);
				}
				s[2] = random.nextInt(10);
				while (s[2] == s[1] || s[2] == s[0])
				{
					s[2] = random.nextInt(10);
				}
				s[3] = random.nextInt(10);
				while (s[3] == s[1] || s[3] == s[0] || s[3] == s[2])
				{
					s[3] = random.nextInt(10);
				}
				*/
				int[] s = new int[4];
				int a, b, c, d, count = 0, count1 = 0, count2 = 0;
				out1.append("Random = "+s[0]+s[1]+s[2]+s[3]);
				head.setText("กรุณาใส่ตัวเลข");
				String b1 = in1.getText().toString();
				String b2 = in2.getText().toString();
				String b3 = in3.getText().toString();
				String b4 = in4.getText().toString();
				if (b1.length() == 0 || b2.length() == 0 || b3.length() == 0
						|| b4.length() == 0)
				{
					head.setText("กรุณาใส่ตัวเลข");
					return;
				}
				a = Integer.parseInt(b1);
				b = Integer.parseInt(b2);
				c = Integer.parseInt(b3);
				d = Integer.parseInt(b4);

				{
					count1 = 0;
					count = 0;
					while (count == 0)
					{
						if (a == s[0])
						{
							out1.append("X");
							count = 1;
							count1 = count1 + 1;
							break;
						}
						if (a == s[1])
						{
							out1.append("/");
							count = 1;
							break;
						}
						if (a == s[2])
						{
							out1.append("/");
							count = 1;
							break;
						}
						if (a == s[3])
						{
							out1.append("/");
							count = 1;
							break;
						} else
						{
							break;
						}
					}
					
				}
				count = 0;
				while (count == 0)
				{
					if (b == s[0])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (b == s[1])
					{
						out1.append("X");
						count1 = count1 + 1;
						count = 1;
						break;
					}
					if (b == s[2])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (b == s[3])
					{
						out1.append("/");
						count = 1;
						break;
					} else
					{
						break;
					}
				}
				count = 0;
				while (count == 0)
				{
					if (c == s[0])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (c == s[1])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (c == s[2])
					{
						out1.append("X");
						count1 = count1 + 1;
						count = 1;
						break;
					}
					if (c == s[3])
					{
						out1.append("/");
						count = 1;
						break;
					}
					else
					{
						break;
					}
				}
				count = 0;
				while (count == 0)
				{
					if (d == s[0])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (d == s[1])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (d == s[2])
					{
						out1.append("/");
						count = 1;
						break;
					}
					if (d == s[3])
					{
						out1.append("X");
						count1 = count1 + 1;
						count = 1;
						break;
					} 
					else
					{
						break;
					}
				}
				if (count1 == 4)
				{
					out1.append("\nPoint = " + count2 + 1 + "\n");
					return;
				}
			}
		});
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


เราต้องการให้เมื่อกดปุ่มแล้วRandomแค่ครั้งเดียวพอครั้งต่อไปไม่ต้องRandomใหม่ครับทำไง



Tag : Mobile, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-12-16 21:14:28 By : thammachet View : 1043 Reply : 1
 

 

No. 1



โพสกระทู้ ( 98 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook

สร้างตัวแปรมาเก็บ มันซักตัวครับ เช่น boolean isClicked เอาไว้เก็บ true/false เวลาแรนดอม หรือไม่งั้น เวลากด Button แรนดอมแล้ว ก็ disabled button ซะ

ผมไม่แน่ใจจุดประสงค์ว่าจะทำอะไรนะครับ โค๊ดยาว ไม่ทันได้ดู






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-12-16 23:25:09 By : devahoy
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - ต้องการให้กดปุ่มแล้ว Random ครั้งเดียวทำไงครับ กดปุ่มครั้งต่อไปให้มันไม่ต้องRandomอีก
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่