Android 尝试在其他按钮上设置onClick()
我是android开发的新手。首先,我使用AsyncTask通过一个按钮将数据保存在数据库中。现在我尝试调用同一活动中其他按钮的意图,但当我单击第二个按钮时,什么也没发生。有人能帮我吗。。这里我也发布了一些代码 MainActivity.javaAndroid 尝试在其他按钮上设置onClick(),android,sqlite,android-intent,android-asynctask,Android,Sqlite,Android Intent,Android Asynctask,我是android开发的新手。首先,我使用AsyncTask通过一个按钮将数据保存在数据库中。现在我尝试调用同一活动中其他按钮的意图,但当我单击第二个按钮时,什么也没发生。有人能帮我吗。。这里我也发布了一些代码 MainActivity.java //initialize all view objects et=(EditText)findViewById(R.id.editText1); AddBtn=(Button)findViewById(R.id.btn_add);
//initialize all view objects
et=(EditText)findViewById(R.id.editText1);
AddBtn=(Button)findViewById(R.id.btn_add);
spn = (Spinner) findViewById(R.id.spinner1);
NextBtn=(Button) findViewById(R.id.button1);
SQLcon = new SQLController(this);
// opening database
SQLcon.open();
loadtospinner();
}
public void onClick(View v){
if(v.getId() == R.id.btn_add){
new MyAsync().execute();
}
else {
Intent in=new Intent(Intent, Second.class);
startActivity(in);
}
}
public void loadtospinner() {
Cursor c = SQLcon.readData();
ArrayList<String> al = new ArrayList<String>();
c.moveToFirst();
while (!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(DBhelper.MEMBER_NAME));
al.add(name);
c.moveToNext();
}
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1,
al);
spn.setAdapter(aa1);
// closing database
SQLcon.close();
}
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
PD = new ProgressDialog(MainActivity.this);
PD.setTitle("Please Wait..");
PD.setMessage("Loading...");
PD.setCancelable(false);
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
String name = et.getText().toString();
// opening database
SQLcon.open();
// insert data into table
SQLcon.insertData(name);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
loadtospinner();
PD.dismiss();
}
}
}
//初始化所有视图对象
et=(EditText)findViewById(R.id.editText1);
AddBtn=(按钮)findviewbyd(R.id.btn\u add);
spn=(喷丝器)findViewById(R.id.spinner1);
NextBtn=(按钮)findViewById(R.id.button1);
SQLcon=新的SQLController(此);
//开放数据库
SQLcon.open();
loadtospinner();
}
公共void onClick(视图v){
如果(v.getId()==R.id.btn\u添加){
新建MyAsync().execute();
}
否则{
意向in=新意向(意向,二级);
星触觉(in);
}
}
公共无效loadtospinner(){
游标c=SQLcon.readData();
ArrayList al=新的ArrayList();
c、 moveToFirst();
而(!c.isAfterLast()){
String name=c.getString(c.getColumnIndex(DBhelper.MEMBER_name));
加上(姓名);
c、 moveToNext();
}
ArrayAdapter aa1=新的ArrayAdapter(
getApplicationContext(),R.layout.spinner\u项,R.id.textView1,
al);
设置适配器(aa1);
//关闭数据库
SQLcon.close();
}
私有类MyAsync扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
PD=新建进度对话框(MainActivity.this);
PD.setTitle(“请稍候”);
PD.setMessage(“加载…”);
PD.可设置可取消(假);
PD.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
字符串名称=et.getText().toString();
//开放数据库
SQLcon.open();
//将数据插入表中
SQLcon.insertData(名称);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
loadtospinner();
PD.解散();
}
}
}
如果您传递的是意图而不是活动,请通过更改此选项来传递活动
Intent in=new Intent(Intent, Second.class);
startActivity(in);
试一试
Intent in=new Intent(getApplicationContext(), Second.class);
startActivity(in);
希望这项工作改变以下的事情
public class FirstActivity extends Activity implements OnClickListener {
之后,在创建
secondButton = (Button) findViewById(R.id.rightButton);
secondButton.setOnClickListener(this);
重写下面的方法
@Override
public void onClick(View v) { // Parameter v stands for the view that was clicked.
// getId() returns this view's identifier.
if(v.getId() == R.id.leftButton){
// setText() sets the string value of the TextView
changingTextView.setText("You clicked First");
}else if(v.getId() == R.id.rightButton){
changingTextView.setText("You clicked Second");
}
}
首先
在活动中实现OnClickListener
而不是setOnClickListener
到按钮
AddBtn=(Button)findViewById(R.id.btn_add);
_AddBtn.setOnClickListener(this);
NextBtn=(Button) findViewById(R.id.button1);
_NextBtn.setOnClickListener(this);
现在进入onClick
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
//all your operetion here for this button
new MyAsync().execute();
break;
case R.id.button1:
//write what u want to do with " NextBtn " button click
Intent in=new Intent(this, Second.class);
startActivity(in);
break;
}
}
请发布您的stacktrace!实际上,没有错误。另一个按钮用作虚拟按钮。不,我不调试,因为我不知道如何调试。嗯,然后你写一个日志,观察你的日志猫,你的按钮单击是否有效,我在第一个按钮上调用asyncTask。行吗?谢谢。它终于起作用了。我是新开发人员。您能告诉我如何从第一个屏幕打开所选文本到下一个屏幕的编辑文本吗?从第一个活动将文本值作为字符串传递,并在其他活动中获得相同的文本,并在编辑文本中进行设置。Intent Intent=new Intent(SendingActivity.this,receivingactivity.class);intent.putExtra(“键名”,值);//值,并使用keyName startActivity(intent)在其他活动中检索它们;