我正在尝试点击生成硬编码加扰字的“新单词”按钮。要么(自行车,球,小丑,苹果)。我的应用程序运行,但当我点击按钮没有任何反应。我不知道它是否缺少onClicklistener或什么。任何编码帮助将不胜感激。我的拼写检查按钮可以生成“是的你是对的”或“再试一次”。
主要活动
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 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
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;
}
答案 0 :(得分:0)
您需要添加按钮(在xml布局中)
android:onClick="submitting"
还尝试创建一个新类:
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;
}}
并更改按钮和主要活动:
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("");
}