我想在同一个按钮上一个接一个地在同一个按钮中执行2个任务

时间:2017-11-04 15:06:57

标签: android button android-intent buttonclick

这是我的点击监听器 这将打开我制作的弹出窗口并检查按钮的单击

此外,还可以在弹出关闭代码中添加任务

 b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {



                // get a reference to the already created main layout
                ConstraintLayout mainLayout =  findViewById(R.id.c1);

                // inflate the layout of the popup window
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = inflater.inflate(R.layout.popup_window, null);

                // create the popup window
                int width = LinearLayout.LayoutParams.WRAP_CONTENT;
                int height = LinearLayout.LayoutParams.WRAP_CONTENT;
                boolean focusable = true; // lets taps outside the popup also dismiss it
                final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

                // show the popup window
                popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

                // dismiss the popup window when touched
                popupView.setOnTouchListener(new View.OnTouchListener() {

                    public boolean onTouch(View v, MotionEvent event) {
                        popupWindow.dismiss();

                            return true;
                    }
                });



            }



    });

这是第1号任务: 这用于将邮件发送到我的邮件ID

Intent email = new Intent(android.content.Intent.ACTION_SEND);

        /* Fill it with Data */
                            email.setType("plain/text");
                            email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"jasaniwasim2002@gmail.com"});
                            email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Verification");
                            email.putExtra(android.content.Intent.EXTRA_TEXT, "I accept that i am verily submitting all my details to the developer and would not mind if it is used by the developer and would also like to sing a song : khushkhabri aisi mili hai " + "uchhalne lage hum hawa mein " + "poori huyi dil ki tamanna " + "baRa hi asar tha duaa mein " + "ban-Than ke baal bana ke " + " jootaa polish karwa ke" + us + usr.getText().toString() + "  " + pw + psw.getText().toString() + " naachenge hum ta-ta-thaiyyaa " + "sajan radio... o, o... bajaiyo bajaiyo bajaiyo zara sajan radio... o, o... bajai ke sabhi ko nachaiyo zara sajan radio... o, o.. bajaiyo bajaiyo bajaiyo zara" + "O.. mere dil ka khoya sa tukRa lauTega ik din kabhi iska mujhe tha yakeen iska mujhe tha yakeen, jaana" + "sadqe mein chaahe lag jaaye isko saari umar bhi meri hoga mujhe gham nahi hoga mujhe gham nahi..." + "       afsar ke jaisa ainTha motor-gaaRi mein baiTha aayega mera sipahiya" + "sajan radio... o, o... bajaiyo bajaiyo bajaiyyo zara sajan radio... o, o... bajai ke sabhi ko nachaiyyo zara sajan radio... o, o... bajaiyo bajaiyo bajaiyyo zara pa pa pa purup purrup pa pa pa purup purrup o kaaka o mausi o cycle waale bhaiya" + "ban-Than ke baal bana ke   jootaa polish karwa ke             naachenge hum ta-ta-thaiyyaa                sajan radio... o, o...        bajaiyo bajaiyo bajaiyo zara        sajan radio... o, o...        bajai ke sabhi ko nachaiyo zara        sajan radio... o, o...        bajaiyo bajaiyo bajaiyo zara  " + "The log of system returns the logarithmic value of the app and sends u the static value of act to setOnclicked view and then close the app and submit the values of the text box" + "\n");
startActivity(Intent.createChooser(email, "Send mail..."));

这是任务号。 2: 这是去一个新的课程

     Intent i = new Intent(LoginActivity.this, Exit.class);
            startActivity(i);

2 个答案:

答案 0 :(得分:0)

您可以尝试的一件事是,为您要执行的任务定义两种方法。

首先实现View.OnClickListener。

public class MainActivity extends AppCompatActivity implements View.OnClickListener

现在定义两种方法。

void tasks1(){
Intent email = new Intent(android.content.Intent.ACTION_SEND);

    /* Fill it with Data */
                        email.setType("plain/text");
                        email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"jasaniwasim2002@gmail.com"});
                        email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Verification");
                        email.putExtra(android.content.Intent.EXTRA_TEXT, "I accept that i am verily submitting all my details to the developer and would not mind if it is used by the developer and would also like to sing a song : khushkhabri aisi mili hai " + "uchhalne lage hum hawa mein " + "poori huyi dil ki tamanna " + "baRa hi asar tha duaa mein " + "ban-Than ke baal bana ke " + " jootaa polish karwa ke" + us + usr.getText().toString() + "  " + pw + psw.getText().toString() + " naachenge hum ta-ta-thaiyyaa " + "sajan radio... o, o... bajaiyo bajaiyo bajaiyo zara sajan radio... o, o... bajai ke sabhi ko nachaiyo zara sajan radio... o, o.. bajaiyo bajaiyo bajaiyo zara" + "O.. mere dil ka khoya sa tukRa lauTega ik din kabhi iska mujhe tha yakeen iska mujhe tha yakeen, jaana" + "sadqe mein chaahe lag jaaye isko saari umar bhi meri hoga mujhe gham nahi hoga mujhe gham nahi..." + "       afsar ke jaisa ainTha motor-gaaRi mein baiTha aayega mera sipahiya" + "sajan radio... o, o... bajaiyo bajaiyo bajaiyyo zara sajan radio... o, o... bajai ke sabhi ko nachaiyyo zara sajan radio... o, o... bajaiyo bajaiyo bajaiyyo zara pa pa pa purup purrup pa pa pa purup purrup o kaaka o mausi o cycle waale bhaiya" + "ban-Than ke baal bana ke   jootaa polish karwa ke             naachenge hum ta-ta-thaiyyaa                sajan radio... o, o...        bajaiyo bajaiyo bajaiyo zara        sajan radio... o, o...        bajai ke sabhi ko nachaiyo zara        sajan radio... o, o...        bajaiyo bajaiyo bajaiyo zara  " + "The log of system returns the logarithmic value of the app and sends u the static value of act to setOnclicked view and then close the app and submit the values of the text box" + "\n");
startActivity(Intent.createChooser(email, "Send mail..."));

}

第二种方法

void tasks2(){
Intent i = new Intent(LoginActivity.this, Exit.class);
            startActivity(i);
}

现在将setonClickListener设置为按钮

b.setOnClickListener(this);

现在只需覆盖onClick方法即可完成。

public void onClick(View view) {

        //When b button is clicked
        if(view==b)
        {
            tasks1();
            tasks2();

        }
}

答案 1 :(得分:0)

onCreate()中添加 onClickListener 按钮:

b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageDialog();
            }
});

之后,添加以下方法

 //showing the message here
        private void showMessageDialog() {

            AlertDialog.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
            } else {
                builder = new AlertDialog.Builder(this);
            }
            builder.setTitle("your_title")
                    .setMessage("your_message")
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        //method for sending mail
                            sendMail();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
    }

//sending mail here
    @SuppressLint("RestrictedApi")
    private void sendMail() {
        Intent email = new Intent(android.content.Intent.ACTION_SEND);

        /* Fill it with Data */
        email.setType("plain/text");
        email.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"jasaniwasim2002@gmail.com"});
        email.putExtra(android.content.Intent.EXTRA_SUBJECT, "Verification");
        email.putExtra(android.content.Intent.EXTRA_TEXT, "your_message");
        startActivityForResult(Intent.createChooser(email, "Send mail..."),100,null);
    }



 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //checking for the result
        if (requestCode == 100) {
        //if its ok, then go to next activity
            if (resultCode == RESULT_OK) {
                // success open your activity
                Intent i = new Intent(LoginActivity.this, Exit.class);
                startActivity(i);
            }
        }
    }