jeudi 3 septembre 2015

android button setonclicklistener

I am new on Android programming. This is my MainActivty.java and layout file .I press the buttons but sometimes it works sometimes doesn't. What is the problem here? Thanks for your help

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
    }

}

layout file

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    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>

Aucun commentaire:

Enregistrer un commentaire