【问题标题】:I want to do 2 tasks in the same button one after the other on the same click我想在同一个按钮上一个接一个地在同一个按钮上执行 2 个任务
【发布时间】:2018-04-17 03:36:50
【问题描述】:

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

也可以在弹窗关闭代码中添加任务

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

【问题讨论】:

  • 只需添加一个检查邮件是否发送的条件。如果已发送,则跳转到下一个活动(您的第二个任务)。如果邮件未发送,则通过您选择的操作进行处理
  • 我是初学者,请问您能把条件发给我吗
  • 在那个弹出窗口中你真的做了什么吗?分享该代码
  • 只是显示一条消息
  • 在那种情况下我推荐alert dialog,它比popupwindow更好。我给你看一个示例代码。

标签: android button android-intent buttonclick


【解决方案1】:

在您的 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);
            }
        }
    }

【讨论】:

  • 但从技术上讲,我们不能保证 ACTION_SENT 可以得到确认。如果您想了解更多详细信息,请参考官方文档。
【解决方案2】:

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

首先实现 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();

        }
}

【讨论】:

  • 他正在尝试先发送电子邮件。所以我认为你的解决方案不兼容。
  • 我不确定这是否可行,但如果你想添加两个任务,那么这应该是最好的方法。
  • 我想知道如何检查邮寄任务是否完成
  • 可以在startActivity()之后放一个Toast
  • 在上面的 cmets 中查看我的要点
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 2020-08-12
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多