Android Studio Hakkında

Gewert34456

Centipat
Katılım
27 Ekim 2019
Mesajlar
35
Android'de uygulama yapıyorum. İki oyuncu için rastgele iki farklı sayı ürettim ve bu sayıları toplatıp hangisi büyükse ekran mesajı olarak yazdıramıyorum. Yardım edebilir misiniz?
 
Eğer kodlama bilginiz yoksa ve basit bir uygulama yapacaksanız ben Android Studio ile Java kodlamak yerine App Inventor kullanmanızı tavsiye ediyorum.
 
Android'de kodluyorum. Yardımcı olacaksanız sevinirim😌
Ekran mesajından kasıt nedir? Toast falan mı? Tam olarak sorununuz nedir? İki sayının sonucunu da iki ayrı değişkene atayacaksınız, daha sonra if-else yapısı ile ilk oyuncu kazanmış ise, if'de bununla ilgili bilgi vereceksiniz, else'te de ikinci oyuncu ile ilgili bilgi vereceksiniz.
Evet toastla ekran mesajı vereceğim. Ama ayrı ayrı toplatıp ekran mesajı veremedim. Bir yerde hatam var. Degişkeni atadım ama hata veriyor.
 
Evet toast'la ekran mesajı vereceğim ama ayrı ayrı toplatıp ekran mesajı veremedim. Bir yerde hatam var.
Kodları buraya ekleyebilir misiniz?

Mesaj kutucuğunun üstünde bulunan butonlardan "..." şeklinde olanın içerisinden "kod"u seçin, yukarıdaki drop down menüden Java veya Kotlin'i seçin, daha sonra kodlarınızı oraya yapıştırın.
 
Son düzenleme:
Kodları buraya ekleyebilir misiniz?

Mesaj kutucuğunun üstünde bulunan butonlardan "..." şeklinde olanın içerisinden "kod"u seçin, yukarıdaki drop down menüden Java veya Kotlin'i seçin, daha sonra kodlarınızı oraya yapıştırın.
Java:
public class MainActivity extends AppCompatActivity {
    EditText edt,edt1,edt2;
    TextView o1,o2;
    Button ras,tem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt=(EditText)findViewById(R.id.editText);
        edt1=(EditText)findViewById(R.id.editText2);
        edt2=(EditText)findViewById(R.id.editText3);
        o1=(TextView)findViewById(R.id.textView);
        o2=(TextView)findViewById(R.id.textView2);
        tem=(Button)findViewById(R.id.button2);
        ras=(Button)findViewById(R.id.button);




        tem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
                edt2.setText("");
            }
        });


        ras.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int say;
                Random rnd=new Random();
                say=rnd.nextInt(10)+1;
                edt1.setText(String.valueOf(say));

                int say2;
                Random rnd2=new Random();
                say2=rnd2.nextInt(10)+1;
                edt2.setText(String.valueOf(say2));

                

            }
        });
Kodları buraya ekleyebilir misiniz?

Mesaj kutucuğunun üstünde bulunan butonlardan "..." şeklinde olanın içerisinden "kod"u seçin, yukarıdaki drop down menüden Java veya Kotlin'i seçin, daha sonra kodlarınızı oraya yapıştırın.
Java:
public class MainActivity extends AppCompatActivity {
    EditText edt,edt1,edt2;
    TextView o1,o2;
    Button ras,tem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt=(EditText)findViewById(R.id.editText);
        edt1=(EditText)findViewById(R.id.editText2);
        edt2=(EditText)findViewById(R.id.editText3);
        o1=(TextView)findViewById(R.id.textView);
        o2=(TextView)findViewById(R.id.textView2);
        tem=(Button)findViewById(R.id.button2);
        ras=(Button)findViewById(R.id.button);




        tem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
                edt2.setText("");
            }
        });


        ras.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int say;
                Random rnd=new Random();
                say=rnd.nextInt(10)+1;
                edt1.setText(String.valueOf(say));

                int say2;
                Random rnd2=new Random();
                say2=rnd2.nextInt(10)+1;
                edt2.setText(String.valueOf(say2));

                if(say>say2)
                {
                    Toast.makeText(getApplicationContext(), "oyuncu1 kazandı", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "oyuncu2 kazandı", Toast.LENGTH_LONG).show();

                }


            }
        });

Son şekli bu oldu herhalde yardımcı olabilir misiniz?
 
Son düzenleme:
Java:
public class MainActivity extends AppCompatActivity {
    EditText edt,edt1,edt2;
    TextView o1,o2;
    Button ras,tem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt=(EditText)findViewById(R.id.editText);
        edt1=(EditText)findViewById(R.id.editText2);
        edt2=(EditText)findViewById(R.id.editText3);
        o1=(TextView)findViewById(R.id.textView);
        o2=(TextView)findViewById(R.id.textView2);
        tem=(Button)findViewById(R.id.button2);
        ras=(Button)findViewById(R.id.button);




        tem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
                edt2.setText("");
            }
        });


        ras.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int say;
                Random rnd=new Random();
                say=rnd.nextInt(10)+1;
                edt1.setText(String.valueOf(say));

                int say2;
                Random rnd2=new Random();
                say2=rnd2.nextInt(10)+1;
                edt2.setText(String.valueOf(say2));

              

            }
        });

Java:
public class MainActivity extends AppCompatActivity {
    EditText edt,edt1,edt2;
    TextView o1,o2;
    Button ras,tem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt=(EditText)findViewById(R.id.editText);
        edt1=(EditText)findViewById(R.id.editText2);
        edt2=(EditText)findViewById(R.id.editText3);
        o1=(TextView)findViewById(R.id.textView);
        o2=(TextView)findViewById(R.id.textView2);
        tem=(Button)findViewById(R.id.button2);
        ras=(Button)findViewById(R.id.button);




        tem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
                edt2.setText("");
            }
        });


        ras.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int say;
                Random rnd=new Random();
                say=rnd.nextInt(10)+1;
                edt1.setText(String.valueOf(say));

                int say2;
                Random rnd2=new Random();
                say2=rnd2.nextInt(10)+1;
                edt2.setText(String.valueOf(say2));

                if(say>say2)
                {
                    Toast.makeText(getApplicationContext(), "oyuncu1 kazandı", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "oyuncu2 kazandı", Toast.LENGTH_LONG).show();

                }


            }
        });
Rastgele iki farklı sayı üretiyorum ama her defasında üretilen sayı hangisiyse onu büyük herhalde yardımcı olabilir misiniz?
Layout'u da buraya kopyalayabilir misiniz acaba? Ekle>Kod>XML.

Değişken isimlerini daha belirgin yaparsanız, daha sonradan baktığınızda, "Bu neydi ya?" demezsiniz. Örnek olarak kaydet butonumuz olsun; kaydetButonu veya saveButton gibi.
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="134dp"
        android:layout_height="46dp"
        android:fontFamily="@font/black_ops_one"
        android:text="OYUNCU1"
        android:textColor="#00796B"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.27"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.306" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="204dp"
        android:layout_height="47dp"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.086" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="136dp"
        android:layout_height="41dp"
        android:layout_marginTop="184dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.854"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.316" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="150dp"
        android:layout_height="42dp"
        android:layout_marginTop="184dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.099"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.316" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="144dp"
        android:layout_height="46dp"
        android:fontFamily="@font/black_ops_one"
        android:text="OYUNCU2"
        android:textColor="#00796B"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.85"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.306" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="72dp"
        android:layout_marginBottom="152dp"
        android:text="rastgele"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="288dp"
        android:layout_marginBottom="164dp"
        android:text="temizle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
 
Amacınıza ulaşmışsınız gibi gözüküyor, herhangi bir sorun yok. En başta if-else yapısı eksikti onu da eklemişsiniz. Sadece en baştaki EditText ne işe yarıyor onu anlayamadım.

Ayrıca şu düzenlemeyi yapmanızı öneriyorum:
Java:
if (say > say2) {
    Toast.makeText(getApplicationContext(), "Oyuncu 1 kazandı", Toast.LENGTH_SHORT).show();
} else if (say2 > say) {
    Toast.makeText(getApplicationContext(), "Oyuncu 2 kazandı", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getApplicationContext(), "Berabere", Toast.LENGTH_SHORT).show();
}

Bu şekilde berabere kaldığında, ekrana "Berabere" de yazdırmış olacağız.
 
Amacınıza ulaşmışsınız gibi gözüküyor, herhangi bir sorun yok. En başta if-else yapısı eksikti onu da eklemişsiniz.
Amacıma tam olarak ulaşamadım.Her defasında hangi sayı büyükse onu kazandı olarak gösteriyor.Oyuncu1'in rastgele sayı toplamıyla oyuncu2'nin sayı toplamı hangisi büyük küçük olucak döngüye sokamadım.Anlatabildim mi.Edittexte hangisinin puanı yüksekse oraya oyuncu1 yada oyuncu2 yazması gerekiyo.Daha sonra mesaj vermesi gerekiyo.
 
Uyarı! Bu konu 7 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Technopat Haberler

Geri
Yukarı