0

我的 android 项目有问题。我无法访问我的按钮。我附上了我的课程代码==http://pastebin.com/A5ZTBkhd。请任何人帮助我。这是我的代码 - 包 com.droid.androiddoctor;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidDoctorMainActivity extends Activity implements OnClickListener{



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_doctor_main);


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
        return true;
    }




    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.btnSubmit:

            String e="hello";
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Dang it!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
            break;
        }
    }


}

.这是我的代码。当我推送提交时,没有任何反应。xml文件是====

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >








    <Button
        android:id="@+id/btnSubmit"
        androi:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

    <Button
        android:id="@+id/btnExit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Exit" />

</LinearLayout>
4

2 回答 2

2

您根本没有为您的小部件设置侦听器。试试这个:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_android_doctor_main);
    Button button1 = (Button) findViewById(R.id.btnSubmit);
    button1.setOnClickListener(this)
}

我建议您阅读Android 的 UI 指南。

于 2012-09-10T20:52:25.020 回答
0

我不会像那样实现 OnClickListener ,尝试像这样在你的类中添加它:

public class AndroidDoctorMainActivity extends Activity {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_doctor_main);
 Button button1 = (Button) findViewById(R.id.btnSubmit);
 Button button2 = (Button) findViewById(R.id.btnExit);

button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});

button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button 

//or if you just want to exit from activity, just call:
finish();
}});

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    }


}
于 2012-09-11T11:25:00.147 回答