android按钮setonclicklistener

时间:2015-09-03 11:48:31

标签: android if-statement button onclicklistener

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

mainactivity

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>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

使用此方法代替您的方法:

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

否则,在{x}中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

最终编辑

length=5; index=-1如果您调试了代码,那么您的问题并不适合{1}}您知道当您按错了答案时,您的索引为-1 = onClick() 所以我已经改变了你的代码,我改进了你的#34;游戏&#34;,看一看。

您的 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()); } } 方法应该是:

@覆盖     public void onClick(查看v){         尝试{             switch(v.getId()){

IndexQuestion

您还要创建一个名为int IndexQuestion; Boolean gamestart = false; 的全局变量,它是问题的索引。 我还添加了一个全局变量,因为我知道游戏是否已经启动,因为当我点击动词时毫无疑问它会说&#34; TRUE&#34;或者&#34; FALSE&#34;我避免它。 创建那些全局变量:

{{1}}

注意:这只是游戏的骨架,您可以根据需要进行更改

答案 1 :(得分:1)

不要在button.ispressed()方法中使用v.getid()使用onclick(),而是使用带有id的按钮大小的switch语句,如下所示:

@Override
public void onClick(View v) {
  switch(v.getId()){
     case R.id.start :
     //Do whatever you want to do here
     break;
     case R.id.button1:
     //Do whatever you want to do here
     break;
}

答案 2 :(得分:0)

第一点:

请勿使用verb.isPressed()来确定按下了哪个按钮。

使用视图ID:

@Override
public void onClick(View v) {

    if (v.getId() == R.id.button1) {

    }else if(v.getId() == R.id.button2) {

    }else if(...){} // and so on

第二点:

这是错误的:

 if(vocabularyType[y]=="noun")

比较字符串时,你应该像这样使用String.equals():

 if(vocabularyType[y].equals("noun"))

答案 3 :(得分:0)

您可以像这样设置OnClickListeners:

yourButton.setOnClickListener(new    Button.OnClickListener() {
    @Override
    public void onClick(View v) {
    //Do something
    }
    });

只需将其放在onCreate方法

答案 4 :(得分:0)

audioimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                try
                {
//Do Something
}
catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });