Onclicklistener的编程创建的按钮

Onclicklistener的编程创建的按钮

本文介绍了Onclicklistener的编程创建的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过了这个答案,但似乎无法找到一个。

我已经程序创建的(而不是在XML文件)一个按钮,我想要的东西时,其点击的情况发生,显示一个警告或移动到另一个屏幕等。

按钮code:

 按钮提交按钮=新按钮(本);
submitButton.setId(99);
submitButton.setText(提交);

监听器code:

 查看submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(本);

所以我已经设置了读心人发现按钮,但我得到这个错误

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 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);

    }
}
解决方案

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.

这篇关于Onclicklistener的编程创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:07