Onclicklistener的编程创建的按钮 [英] Onclicklistener for a programatically created button
问题描述
我已经看过了这个答案,但似乎无法找到一个。
我已经程序创建的(而不是在XML文件)一个按钮,我想要的东西时,其点击的情况发生,显示一个警告或移动到另一个屏幕等。
按钮code:
按钮提交按钮=新按钮(本);
submitButton.setId(99);
submitButton.setText(提交);
监听器code:
查看submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(本);
所以我已经设置了读心人发现按钮,但我得到这个错误
在类型视图的方法setOnClickListener(View.OnClickListener)
是不适用的参数
(RegisterScreen)RegisterScreen.java / AccessibleApp / src目录/组织/项目/访问线
217 Java问题
块引用>我想这可能是一些做的事实,我没有
的setContentView();
在类的开始,因为整个页写编程(因为我需要添加复选框编程)。下面是下面的所有code,如果有帮助:
公共类RegisterScreen延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//启动数据库被qureied
DatabaseData DB =新DatabaseData(本);
DB =新DatabaseData(本);
尝试{
db.createDataBase();
}
赶上(IOException异常IOE){
抛出新的错误(无法创建数据库);
}
尝试{
db.openDataBase();
}
赶上(的SQLException SQLE){
扔SQLE;
}
SQLiteDatabase RDB = db.getReadableDatabase(); //屏幕的主要布局
的LinearLayout layoutMain =新的LinearLayout(本);
layoutMain.setOrientation(LinearLayout.VERTICAL);
layoutMain.setBackgroundColor(Color.WHITE); //线性布局横幅
的LinearLayout旗帜=新的LinearLayout(本);
banner.setOrientation(LinearLayout.VERTICAL);
banner.setBackgroundColor(Color.rgb(17,168,191)); //布局PARAMS的高度和宽度
的LayoutParams bannerParams =新android.widget.LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.FILL_PARENT,40);
banner.setLayoutParams(bannerParams); //横幅文字
TextView的bannerText =新的TextView(本);
bannerText.setText(注册);
bannerText.setTextColor(Color.WHITE);
banner.addView(bannerText);
bannerText.setTextSize(24);
bannerText.setGravity(Gravity.CENTER); //添加旗帜布局主要布局
layoutMain.addView(旗); 在屏幕的其余部分//滚动视图
滚动型SV =新的滚动型(本);
//表布局调整项目登记表项目
TableLayout TL =新TableLayout(本); //表行把页面的左,右两侧的项目
的TableRow usernameTR =新的TableRow(本); //用户名标签
TextView的usernameL =新的TextView(本);
usernameL.setText(用户名:);
usernameL.setTextColor(Color.BLACK);
usernameTR.addView(usernameL); //用户名文本框
的EditText usernameTB =新的EditText(本);
usernameTB.setWidth(250);
usernameTR.addView(usernameTB);
tl.addView(usernameTR); 的TableRow emailTR =新的TableRow(本); //邮件标签
TextView的emailL =新的TextView(本);
emailL.setText(电子邮件:);
emailL.setTextColor(Color.BLACK);
emailTR.addView(emailL); //电子邮件文本框
的EditText emailTB =新的EditText(本);
emailTB.setWidth(250);
emailTR.addView(emailTB);
tl.addView(emailTR); 的TableRow forenameTR =新的TableRow(本); //用名字标签
TextView的forenameL =新的TextView(本);
forenameL.setText(用名字:);
forenameL.setTextColor(Color.BLACK);
forenameTR.addView(forenameL); //用名字文本框
的EditText forenameTB =新的EditText(本);
forenameTB.setWidth(250);
forenameTR.addView(forenameTB);
tl.addView(forenameTR); 的TableRow surnameTR =新的TableRow(本); //姓标签
TextView的surnameL =新的TextView(本);
surnameL.setText(姓:);
surnameL.setTextColor(Color.BLACK);
surnameTR.addView(surnameL); //姓文本框
的EditText surnameTB =新的EditText(本);
surnameTB.setWidth(250);
surnameTR.addView(surnameTB);
tl.addView(surnameTR); 的TableRow streetTR =新的TableRow(本); //街标签
TextView的streetL =新的TextView(本);
streetL.setText(街道);
streetL.setTextColor(Color.BLACK);
streetTR.addView(streetL); //街道文本框
的EditText streetTB =新的EditText(本);
streetTB.setWidth(250);
streetTR.addView(streetTB);
tl.addView(streetTR); 后期的TableRow codeTR =新的TableRow(本); //后code标签
TextView中后codeL =新的TextView(本);
后codeL.setText(邮报code:);
后codeL.setTextColor(Color.BLACK);
后codeTR.addView(后codeL); //后code文本
后期的EditText codeTB =新的EditText(本);
后codeTB.setWidth(250);
后codeTR.addView(后codeTB);
tl.addView(后codeTR); 的TableRow cityTR =新的TableRow(本); //城市标签
TextView的cityL =新的TextView(本);
cityL.setText(城市);
cityL.setTextColor(Color.BLACK);
cityTR.addView(cityL); //城市文本框
的EditText cityTB =新的EditText(本);
cityTB.setWidth(250);
cityTR.addView(cityTB);
tl.addView(cityTR); 的TableRow countyTR =新的TableRow(本); //县标签
TextView的countyL =新的TextView(本);
countyL.setText(县);
countyL.setTextColor(Color.BLACK);
countyTR.addView(countyL); //县文本框
的EditText countyTB =新的EditText(本);
countyTB.setWidth(250);
countyTR.addView(countyTB);
tl.addView(countyTR); //表布局添加滚动视图 //国家下拉
TextView的catTitle =新的TextView(本);
catTitle.setText(\\ n请选择影响您的类别:\\ n);
catTitle.setTextColor(Color.BLACK);
tl.addView(catTitle);
//类别
//类标题 的String [] COLS = {_id,cat_name};要搜索//列
光标光标= rdb.query(aa_category,COLS,NULL,NULL,NULL,NULL,NULL); //查询保存到数据库
而(cursor.moveToNext()){
复选框catCB =新的复选框(本);
字符串名称= cursor.getString(1);
INT ID = cursor.getInt(0);
catCB.setId(ID);
catCB.setText(\\ n+名字+\\ n);
catCB.setTextColor(Color.BLACK);
tl.addView(catCB);
}
//添加字段与文本字段新的类别,将成为活跃在点击复选框
按钮提交按钮=新按钮(本);
submitButton.setId(99);
submitButton.setText(提交); 查看submitButtonListener = findViewById(99); submitButtonListener.setOnClickListener(本); tl.addView(提交按钮); //表布局添加滚动视图
sv.addView(TL);
//滚动视图添加到主布局
layoutMain.addView(SV);
this.setContentView(layoutMain); }
}
解决方案您的活动没有实施View.OnClickListener。您的类定义更改为
公共类RegisterScreen扩展活动实现View.OnClickListener
和添加
公共无效的onClick(视图v){}
你的Activity类。
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 listner 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 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的编程创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!