onButtonClick从未在源代码中使用过?

时间:2016-05-19 07:03:08

标签: java android listview android-edittext onclicklistener

我正在编写一个工作正常的简单应用程序,但问题是如果输入正确,我使用密码条件触发按钮点击。

问题是我的源代码说'onButtonClick'从未使用过,当我在应用程序中手动按下该按钮时,它会突然强制关闭并崩溃。任何人都知道我做错了什么。我在源代码的开头扩展Activity。我应该延长AppCompatActivity吗?

public class Gvoice extends Activity implements OnClickListener{

ListView lv1;
static final int check = 1111;
Button b1;
Button b_home;
EditText a1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gvoice);
    lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
    b1 = (Button)findViewById(R.id.GVoice);
    a1 = (EditText) findViewById(R.id.editTextHome);
    b1.setOnClickListener(this);

    //This now handles an automatic press of the bVoice button 1 second after the activity is opened
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            b1.callOnClick();
        }
    }, 1000);

}

public void onButtonClick(View v) {
    if (v.getId() == R.id.BHome) {
        String str = a1.getText().toString();


        //Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'xxx' which is not case sensitive
        if (str.toLowerCase().contains("home")) {
            Intent userintent = new Intent(Gvoice.this, PocketSphinxActivity.class);
            startActivity(userintent);
        } else {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}

public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
    startActivityForResult(i1, check);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == check && resultCode == RESULT_OK){
        ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
        a1.setText((String) lv1.getItemAtPosition(0));    //Get the first phrase in the first row of list view

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                b_home.performClick();
            }
        }, 500);    //Automatically click the 'Blogin' button after 500ms
    }

    super.onActivityResult(requestCode, resultCode, data);
}

}

更新:下面是xml文件。请注意,onButtonClick已添加到xml文件中,但在使用条件语句单击按钮时仍强行关闭应用程序:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec">

<ImageView
    android:layout_width="100dip"
    android:layout_height="100dip"
    android:background="@drawable/patient_two"
    android:id="@+id/pimage"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="85dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Patient Name: Joe Blogs"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Cause of Injury: Car crash"
    android:id="@+id/textView2"
    android:layout_below="@+id/pimage"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Date of Birth:"
    android:id="@+id/textView3"
    android:layout_below="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginTop="25dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Gender:"
    android:id="@+id/textView4"
    android:layout_below="@+id/textView3"
    android:layout_alignStart="@+id/textView3" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Occupation:"
    android:id="@+id/textView5"
    android:layout_below="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Address:"
    android:id="@+id/textView6"
    android:layout_below="@+id/textView5"
    android:layout_alignStart="@+id/textView5" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medical History"
    android:id="@+id/textView7"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView6"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Heart attack"
    android:id="@+id/textView8"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView7"
    android:layout_alignStart="@+id/textView6" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Arthritis"
    android:id="@+id/textView9"
    android:layout_below="@+id/textView8"
    android:layout_alignStart="@+id/textView8" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Tests Completed"
    android:id="@+id/textView10"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView9"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="X-Ray"
    android:id="@+id/textView11"
    android:layout_below="@+id/textView10"
    android:layout_alignStart="@+id/textView9"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="..."
    android:id="@+id/textView12"
    android:layout_below="@+id/textView11"
    android:layout_alignStart="@+id/textView11" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Tests Due"
    android:id="@+id/textView14"
    android:layout_below="@+id/textView12"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="..."
    android:id="@+id/textView15"
    android:layout_below="@+id/textView14"
    android:layout_alignStart="@+id/textView12"
    android:layout_marginTop="15dp" />

<ListView
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:id="@+id/lvVoiceReturn1"
    android:textColor="@color/white"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter"
    android:id="@+id/Blogin1"
    android:onClick="onButtonClick"
    android:layout_alignParentBottom="true"
    android:layout_toStartOf="@+id/bVoice1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Speak"
    android:id="@+id/bVoice1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/TFusername1"
    android:layout_alignParentStart="true"
    android:hint="Speech to Text" />

2 个答案:

答案 0 :(得分:1)

您可以通过4方法处理按钮点击:

方法1:

public class Mtest extends Activity {

  Button b1;
  public void onCreate(Bundle savedInstanceState) {
    ...
   Button b1 = (Button) findViewById(R.id.b1);
   b1.setOnClickListener(myhandler1);
     ...
  }
  View.OnClickListener myhandler1 = new View.OnClickListener() {
    public void onClick(View v) {
      // it was the 1st button
    }
  };

}

方法2:

class MTest extends Activity implements OnClickListener {
    public void onCreate(Bundle savedInstanceState) {
    ...
    Button b1 = (Button) findViewById(R.id.b1);

    b1.setOnClickListener(this);
      ...
    }

    @Override
    public void onClick(View v) {

    }
}

xml和android中的方法3:onClick =&#34; HandleClick&#34; :

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="HandleClick" />



public class MTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void HandleClick(View view) {

    }
}

方法4:

public class MTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         Button b1 = (Button) findViewById(R.id.b1);
        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // do stuff
            }
        });

    }
}

答案 1 :(得分:0)

问题在于最初没有在xml文件中添加onButtonClick,也没有在java文件中正确分配按钮标签。

    lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
    b1 = (Button)findViewById(R.id.GVoice);
    a1 = (EditText) findViewById(R.id.editTextHome);
    c1 = (Button)findViewById(R.id.BHome);

    b1.setOnClickListener(this);

以下更正了问题,现在一切正常。希望这可以在将来帮助其他人