Onclicklistener用于以编程方式创建的按钮

时间:2011-02-18 15:01:02

标签: android button

我已经找到了答案,但似乎找不到答案。

我有一个以编程方式创建的按钮(而不是在xml文件中),我希望在点击它时显示某些内容,显示警告或移动到另一个屏幕等。

按键代码:

Button submitButton = new Button(this);
submitButton.setId(99);
submitButton.setText("Submit");

听众代码:

View submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(this);

所以我设置了监听器以找到该按钮,但我收到此错误

  

View类型中的方法setOnClickListener(View.OnClickListener)   不适用于参数   (RegisterScreen)RegisterScreen.java / AccessibleApp / src / org / project / accessible line   217 Java问题

我认为这可能与我在课程开始时没有setContentView();的事实有关,因为整个页面是以编程方式编写的(因为我需要以编程方式添加复选框)。

以下是所有代码,如果它有帮助:

public class RegisterScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        //initiate the database to be qureied
        DatabaseData db = new DatabaseData(this);
        db = new DatabaseData(this);
        try {
            db.createDataBase();
        } 
        catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try { 
            db.openDataBase();
        }
        catch(SQLException sqle){
            throw sqle;
        }
        SQLiteDatabase rdb = db.getReadableDatabase();

        //main layout of the screen     
        LinearLayout layoutMain = new LinearLayout(this);
        layoutMain.setOrientation(LinearLayout.VERTICAL);
        layoutMain.setBackgroundColor(Color.WHITE);

        //Linear Layout for the Banner
        LinearLayout banner = new LinearLayout(this);
        banner.setOrientation(LinearLayout.VERTICAL);
        banner.setBackgroundColor(Color.rgb(17, 168, 191));

        //layout params for height and width
        LayoutParams bannerParams = new android.widget.LinearLayout.LayoutParams(
                android.widget.LinearLayout.LayoutParams.FILL_PARENT, 40);
        banner.setLayoutParams(bannerParams);

        //Banner text
        TextView bannerText = new TextView(this);
        bannerText.setText("Register");
        bannerText.setTextColor(Color.WHITE);
        banner.addView(bannerText);
        bannerText.setTextSize(24);
        bannerText.setGravity(Gravity.CENTER);

        //add banner layout to main layout
        layoutMain.addView(banner);

        //Scroll view for the rest of the screen
        ScrollView sv = new ScrollView(this);


        //Table layout to align the items register form items
        TableLayout tl = new TableLayout(this);

        //Table rows to put items on left and right sides of the page
        TableRow usernameTR = new TableRow(this);

        //Username label
        TextView usernameL = new TextView(this);
        usernameL.setText("Username:");
        usernameL.setTextColor(Color.BLACK);
        usernameTR.addView(usernameL);

        //Username textbox
        EditText usernameTB = new EditText(this);
        usernameTB.setWidth(250);
        usernameTR.addView(usernameTB); 
        tl.addView(usernameTR);

        TableRow emailTR = new TableRow(this);

        //email label
        TextView emailL = new TextView(this);
        emailL.setText("Email:");
        emailL.setTextColor(Color.BLACK);
        emailTR.addView(emailL);

        //email textbox
        EditText emailTB = new EditText(this);
        emailTB.setWidth(250);
        emailTR.addView(emailTB);   
        tl.addView(emailTR);

        TableRow forenameTR = new TableRow(this);

        //forename label
        TextView forenameL = new TextView(this);
        forenameL.setText("Forename:");
        forenameL.setTextColor(Color.BLACK);
        forenameTR.addView(forenameL);

        //forename textbox
        EditText forenameTB = new EditText(this);
        forenameTB.setWidth(250);
        forenameTR.addView(forenameTB); 
        tl.addView(forenameTR);

        TableRow surnameTR = new TableRow(this);

        //surname label
        TextView surnameL = new TextView(this);
        surnameL.setText("Surname:");
        surnameL.setTextColor(Color.BLACK);
        surnameTR.addView(surnameL);

        //surname textbox
        EditText surnameTB = new EditText(this);
        surnameTB.setWidth(250);
        surnameTR.addView(surnameTB);   
        tl.addView(surnameTR);

        TableRow streetTR = new TableRow(this);

        //street label
        TextView streetL = new TextView(this);
        streetL.setText("Street:");
        streetL.setTextColor(Color.BLACK);
        streetTR.addView(streetL);

        //street textbox
        EditText streetTB = new EditText(this);
        streetTB.setWidth(250);
        streetTR.addView(streetTB); 
        tl.addView(streetTR);

        TableRow postcodeTR = new TableRow(this);

        //postcode label
        TextView postcodeL = new TextView(this);
        postcodeL.setText("Postcode:");
        postcodeL.setTextColor(Color.BLACK);
        postcodeTR.addView(postcodeL);

        //postcode textbox
        EditText postcodeTB = new EditText(this);
        postcodeTB.setWidth(250);
        postcodeTR.addView(postcodeTB); 
        tl.addView(postcodeTR);

        TableRow cityTR = new TableRow(this);       

        //city label
        TextView cityL = new TextView(this);
        cityL.setText("City:");
        cityL.setTextColor(Color.BLACK);
        cityTR.addView(cityL);

        //city textbox
        EditText cityTB = new EditText(this);
        cityTB.setWidth(250);
        cityTR.addView(cityTB); 
        tl.addView(cityTR);

        TableRow countyTR = new TableRow(this);

        //county label
        TextView countyL = new TextView(this);
        countyL.setText("County:");
        countyL.setTextColor(Color.BLACK);
        countyTR.addView(countyL);

        //county textbox
        EditText countyTB = new EditText(this);
        countyTB.setWidth(250);
        countyTR.addView(countyTB); 
        tl.addView(countyTR);

        //Add table layout to the scroll view

        //country dropdown  
        TextView catTitle = new TextView(this);
        catTitle.setText("\nPlease select the categories which affect you:\n");
        catTitle.setTextColor(Color.BLACK);
        tl.addView(catTitle);
        //categories
        //categories title

        String[] cols =  {"_id", "cat_name"}; //columns to be searched
        Cursor cursor = rdb.query("aa_category", cols, null, null, null, null, null);   // save the query to the db
        while (cursor.moveToNext()) { 
            CheckBox catCB = new CheckBox(this);
            String name = cursor.getString(1);
            int id = cursor.getInt(0);
            catCB.setId(id);
            catCB.setText("\n"+name+"\n");
            catCB.setTextColor(Color.BLACK);
            tl.addView(catCB);
        }
        //add field for new category with a text field that will become active on clicking the checkbox
        Button submitButton = new Button(this);
        submitButton.setId(99);
        submitButton.setText("Submit");

        View submitButtonListener = findViewById(99);

        submitButtonListener.setOnClickListener(this);

        tl.addView(submitButton);

        //Add table layout to the scroll view
        sv.addView(tl);
        //Add scroll view to the main layout
        layoutMain.addView(sv);
        this.setContentView(layoutMain);
    }
}

2 个答案:

答案 0 :(得分:2)

您的活动未实现View.OnClickListener。将班级定义更改为

public class RegisterScreen extends Activity implements View.OnClickListener

并添加

public void onClick(View v) {

}

到你的Activity类。

答案 1 :(得分:0)

RegisterScreen应该实现View.OnClickListener接口,即

public class RegisterScreen extends Activity implements View.OnClickListener

然后提供onClick方法的实现。