简体   繁体   English

单词加密器,Android Studio

[英]Word Scrambler , Android Studio

I'm trying to click a "new word" button that generates a hard coded scrambled word. 我试图单击“新单词”按钮,该按钮会生成一个硬编码的加扰单词。 Either (bike, ball, clown, apple).My app runs but when i click the button nothing happens. (自行车,球,小丑,苹果)。我的应用程序运行,但是当我单击按钮时,什么也没有发生。 I don't know if it's lack of an onClicklistener or what. 我不知道它是否缺少onClicklistener或什么。 Any coding help would be appreciated. 任何编码的帮助,将不胜感激。 My spell check button is suppose to generate a "yes you're correct" or "try again" as well. 我的拼写检查按钮应该生成“是的”或“再试一次”。

MAIN ACTIVITY 主要活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
   private Random random;
   private static String[] words = {"clown", "ball", "bike", "apple"};
   private String inputField;
   private String answerField;
   private EditText userAns;
   private TextView scrammbleField;
   private Button newWord;
   private Button spellCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    random = new Random();
    userAns = (EditText)findViewById(R.id.spellAns);
    scrammbleField = (TextView) findViewById(R.id.scrambleWrd);
    newWord = (Button)findViewById(R.id.newWord);
    spellCheck = (Button)findViewById(R.id.spellCheck);

}
public void answer(View view) {
    String input = userAns.getText().toString();
    if(answerField.equalsIgnoreCase(input)){
        Toast.makeText(MainActivity.this,"Correct answer!",Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(MainActivity.this,"Try again!",Toast.LENGTH_LONG).show();

    }
}
public void submitting(View view) {
    int randint = random.nextInt(words.length);
    answerField = words[randint];
    inputField = wordScramble.Scramble(answerField);
    scrammbleField.setText(inputField);
    userAns.setText("");

}
}

XML ACTIVITY XML活动

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hynes.scrambler.MainActivity">

<Button
    android:text="Get A New Word"
    android:layout_height="wrap_content"
    android:layout_marginBottom="84dp"
    android:id="@+id/newWord"
    android:layout_width="500dp"
    android:layout_above="@+id/spellCheck"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="Spelll Check!"
    android:layout_width="500dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="89dp"
    android:id="@+id/spellCheck"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:text="ALPHABETS:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/spellCheck"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginBottom="54dp"
    android:id="@+id/textView2" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@mipmap/ic_launcher"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/scrambleWrd"
    android:layout_alignStart="@+id/scrambleWrd"
    android:layout_marginLeft="19dp"
    android:layout_marginStart="19dp"
    android:layout_marginTop="43dp"
    android:id="@+id/imageView" />

<TextView
    android:text="Spell:"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:layout_above="@+id/spellCheck"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="20dp"
    android:layout_toLeftOf="@+id/scrambleWrd"
    android:layout_toStartOf="@+id/scrambleWrd" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/spellAns"
    android:layout_alignEnd="@+id/spellAns"
    android:layout_marginRight="43dp"
    android:layout_marginEnd="43dp"
    android:layout_marginBottom="21dp"
    android:id="@+id/editText3" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/spellAns"
    android:layout_above="@+id/spellCheck"
    android:layout_toRightOf="@+id/textView2"
    android:layout_toEndOf="@+id/textView2" />

</RelativeLayout>

WORDSCRAMBLER.java WORDSCRAMBLER.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;



public class wordScramble {
public static void main(String []args){
    Random rand = new Random();
    String[] words = {"clown", "ball", "bike", "apple"};
    int randint = rand.nextInt(words.length);
    String Pickword = words[randint];
    Scramble(Pickword);
}


public static String Scramble(String ch)
{
    ArrayList<Character> array = new ArrayList<Character>();
    String Scrambled = "";
    for (int b = 0; b < ch.length() ; b++){
        array.add(ch.charAt(b));
    }
    Collections.shuffle(array);
    for (int k = 0; k < array.size(); k++)
    {
        Scrambled += array.get(k);
    }
    return Scrambled;
}

You need to add to the button (in the xml layout) 您需要添加到按钮(在xml布局中)

 android:onClick="submitting"

Also try creating a new class: 也尝试创建一个新类:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class WordScramble2 {

static String pickword;


public WordScramble2()
{
    Random rand = new Random();
    String[] words = {"clown", "ball", "bike", "apple"};
    int randint = rand.nextInt(words.length);
    pickword = words[randint];

}



  public static String scramble(String ch)
{
    ArrayList<Character> array = new ArrayList<Character>();
    String Scrambled = "";
    for (int b = 0; b < ch.length() ; b++){
        array.add(ch.charAt(b));
    }
    Collections.shuffle(array);
    for (int k = 0; k < array.size(); k++)
    {
        Scrambled += array.get(k);
    }
    return Scrambled;
}}

and change the button and main activity: 并更改按钮和主要活动:

public void submitting(View view) {
int randint = random.nextInt(words.length);
answerField = words[randint];
WordScramble2 wrdScr2 = new WordScramble2();
inputField =   wrdScr2.scramble(answerField);
scrammbleField.setText(inputField);
userAns.setText("");

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM