【问题标题】:Trying to set onClick() on other buttons试图在其他按钮上设置 onClick()
【发布时间】:2014-04-25 06:25:49
【问题描述】:

我是 android 开发的新手。首先,我使用 AsyncTask 使用按钮将数据保存在数据库中。现在我试图在同一活动中的其他按钮上调用意图,但是当我点击第二个按钮时没有任何反应。任何人都可以帮我PLZ ..在这里我也发布了一些代码。

MainActivity.java

     //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();
        }



    }

}

【问题讨论】:

  • 请发布您的堆栈跟踪!
  • 其实没有错误。另一个按钮用作虚拟按钮。

标签: android sqlite android-intent android-asynctask


【解决方案1】:

您活动中的第一个 implements OnClickListener

setOnClickListenerbutton 喜欢

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


    }

【讨论】:

  • 谢谢。它终于起作用了。我是新开发人员。你能告诉我如何将选定的文本从第一个屏幕打开到下一个屏幕的编辑文本吗?
  • 将文本值作为字符串从第一个活动传递,并在其他活动中获取相同的文本并在编辑文本中设置。
  • Intent intent = new Intent(SendingActivity.this,RecievingActivity.class); intent.putExtra("keyName", value); // 值并在另一个 Activity 中使用 keyName startActivity(intent); 检索它们
【解决方案2】:

改变下面的东西

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

【讨论】:

  • 我在第一个按钮上调用 asyncTask。它会起作用吗?
【解决方案3】:

你传递的是 Intent 而不是 Activity,通过 Change this 传递 Activity

Intent  in=new Intent(Intent, Second.class);
        startActivity(in);

试试看

Intent  in=new Intent(getApplicationContext(), Second.class);
            startActivity(in);

希望这项工作

【讨论】:

  • 不,我不调试,因为我不知道如何调试。
  • 嗯,然后你写一个日志,看看你的日志猫,你的按钮点击是否有效
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多