开发者

Onclicklistener for a programmatically created button

I have looked for an answer for this but can't seem to find one.

I have a button that has been created programmatically (rather than in the xml file) and I want something to happen when its clicked, show an alert or move to another screen etc.

Button code:

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

Listener code:

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

So I have set up the listener to find that button but I get this error

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (RegisterScreen) RegisterScreen.java /AccessibleApp/src/org/project/accessible line 217 Java Problem

I think it may be something to do with the fact that I don't have setContentView(); at the start of the class because the whole page is written programmatically (because I need to add checkboxes programmatically).

Here is the all the code below if it helps:

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 lay开发者_JAVA百科out 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);
    }
}


Your activity isn't implementing View.OnClickListener. Change your class definition to

public class RegisterScreen extends Activity implements View.OnClickListener

and add

public void onClick(View v) {

}

to your Activity class.


RegisterScreen should implement the View.OnClickListener interface i.e.

public class RegisterScreen extends Activity implements View.OnClickListener

and then provide an implementation for the onClick method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜