android按钮setonclicklistener

标签 android if-statement button onclicklistener

我是 Android 编程新手。这是我的 MainActivty.java 和布局文件。我按下按钮,但有时它起作用有时不起作用。这里有什么问题?感谢您的帮助

主要 Activity

public class MainActivity extends Activity implements OnClickListener {

Button mix,verb,noun,adjective,adverb,meaning;
TextView screen, result, checkmeaning;
ExpandableListView list;
String[] vocabulary = { "open", "computer", "get up", "good", "carefully" };
String[] vocabularyType = { "verb", "noun", "verb", "adjective", "adverb" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupButtons();


}



public int karistir() {

    Random rnd = new Random();
    int sayi;
    sayi = rnd.nextInt(vocabulary.length);
    return sayi;
}

private void setupButtons() {
    list = (ExpandableListView) findViewById(R.id.expandableListView1);
    screen = (TextView) findViewById(R.id.screen);
    result = (TextView) findViewById(R.id.result);
    checkmeaning = (TextView) findViewById(R.id.textView1);
    mix = (Button) findViewById(R.id.start);
    verb = (Button) findViewById(R.id.button1);
    noun = (Button) findViewById(R.id.button2);
    adjective= (Button) findViewById(R.id.button3);
    adverb= (Button) findViewById(R.id.button4);
    meaning= (Button) findViewById(R.id.button5);
    verb.setOnClickListener(this);
    noun.setOnClickListener(this);
    adjective.setOnClickListener(this);
    adverb.setOnClickListener(this);
    meaning.setOnClickListener(this);
    mix.setOnClickListener(this);



}

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

@Override
public void onClick(View v) {
    try {
        if (mix.isPressed()) {
            result.setText("");
            screen.setText("");
            int i = karistir();
            screen.setText(vocabulary[i]);
}

        else if (verb.isPressed()){
            int x = Arrays.binarySearch(vocabulary, screen.getText());

            if(vocabularyType[x]=="verb"){
                result.setText("TRUE");
            }

        }
        else if (noun.isPressed()){
            int x = Arrays.binarySearch(vocabulary, screen.getText());

            if(vocabularyType[x]=="noun"){
                result.setText("TRUE");
            }

        }
        else if( adjective.isPressed()){
            int x = Arrays.binarySearch(vocabulary, screen.getText());

            if(vocabularyType[x]=="adjective"){
                result.setText("TRUE");
            }

        }
        else if (adverb.isPressed()){
            int x = Arrays.binarySearch(vocabulary, screen.getText());

            if(vocabularyType[x]=="adverb"){
                result.setText("TRUE");
            }

        }

    } catch (Exception e) {
        // TODO: handle exception
    }

}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/result"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@+id/start"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="fill_parent"
        android:layout_height="79dp"
        android:layout_weight="1" >

    </ExpandableListView>

    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_weight="1"
        android:text="New Vocabulary"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/screen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="TextView"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Verb" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Noun" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Adjective" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Adverb" />

    </LinearLayout>

<TextView
    android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_weight="1"
    android:text="TextView"
    android:textSize="25sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="TextView"
        android:textSize="30sp" />

</LinearLayout>

</LinearLayout>

最佳答案

用这个方法代替你的:

@Override
public void onClick(View v) {
    int IdButton = v.getId();
    switch (IdButton){
        case R.id.button:
            //Do stuff
        break;
        case R.id.button2:
            //Do stuff
        break;
    }
}

否则,在您的 xml 中的每个 Button 上添加 android:onClick="NounClick"(示例),您的代码应为:

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="NounClick"
        android:text="Noun" />

方法应该是这样的:

public void NounClick(View v) {
        Toast.makeText(MainActivity.this, "NounClicked", Toast.LENGTH_SHORT).show();
        // does something very interesting
}

顺便说一句,如果您使用我的最后一个选项,请确保删除此选项:

verb.setOnClickListener(this);
noun.setOnClickListener(this);
adjective.setOnClickListener(this);
adverb.setOnClickListener(this);
meaning.setOnClickListener(this);
mix.setOnClickListener(this);

最终编辑

您的问题不在于 CLICKS,如果您调试过代码,您会知道当您输入错误答案时,您的索引为 -1 as = length=5;索引=-1 所以我稍微更改了您的代码并改进了您的“游戏”,请看一下。

你的 onClick() 方法应该是这样的:

@覆盖 public void onClick(View v) { 尝试 { 开关(v.getId()){

            case R.id.start:
                gamestart = true;
                result.setText("");
                screen.setText("");
                IndexQuestion = karistir();
                screen.setText(vocabulary[IndexQuestion]);
                Log.d("LLETRA i", String.valueOf(IndexQuestion));
                break;
            case R.id.button1:
                if(gamestart) {
                    if (vocabularyType[IndexQuestion].equals("verb")) {
                        result.setText("TRUE");
                    } else {
                        result.setText("FALSE");
                    }
                }
                break;
            case R.id.button2:
                if(gamestart) {
                    if (vocabularyType[IndexQuestion].equals("noun")) {
                        result.setText("TRUE");
                    } else {
                        result.setText("FALSE");
                    }
                }
                break;
            case R.id.button3:
                if(gamestart) {
                    if (vocabularyType[IndexQuestion].equals("adjective")) {
                        result.setText("TRUE");
                    } else {
                        result.setText("FALSE");
                    }
                }
                break;
            case R.id.button4:
                if(gamestart) {
                    if (vocabularyType[IndexQuestion].equals("adverb")) {
                        result.setText("TRUE");
                    } else {
                        result.setText("FALSE");
                    }
                }
                break;
            default:
                break;
        }

    } catch (Exception e) {
        // TODO: handle exception
        Log.d(TAG,e.getMessage());
    }

}

并且您必须创建一个名为 IndexQuestion 的全局变量,它是问题的索引。 我还添加了一个全局变量来了解游戏是否开始,因为当我点击一个动词时毫无疑问它会说“真”或“假”,我避免了它。 创建那些全局变量:

int IndexQuestion;
Boolean gamestart = false;

注意:这只是您游戏的骨架,您可以随意更改

关于android按钮setonclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374777/

相关文章:

java - 当 API 通过 JSON 返回 null 值时,使用 Moshi 时有什么方法可以返回 Kotlin 数据类中的默认值

java - 让 android 通知打开新 Activity

python - 为什么我得到 "maximum recursion depth exceeded"

swift - 如何确定表格 View 中按下按钮的单元格

iOS 模拟器无法正确刷新

android - 如何在 Android 中播放音效

android - 为什么 ITelephony.aidl 有效?

javascript - 在 HTML 中的 JavaScript 中插入 HTML

正则表达式双列匹配 R

css - 如何将按钮放在动态大小的div的中心