猿问

如何从 textview Results 获取 Alertdialog 的值

我想当我单击显示警报对话框的按钮“mButtonadd”时,我想在警报对话框上获得“mTextViewResult”的值(这是一个简单的乘法)


private EditText mEditText1;

private TextView mTextViewResult;

private Button mButtonAdd;

int a;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_builderdaily);




    mEditText1 = findViewById(R.id.editText1);

    a = 15;

    mTextViewResult = findViewById(R.id.textView);

    mButtonAdd = findViewById(R.id.button);


    mButtonAdd.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (mEditText1.getText().toString().length() == 0) {

                mEditText1.setText("0");

            }


            int num1 = Integer.parseInt(mEditText1.getText().toString());

            final int sum = num1 * a;

            mTextViewResult.setText(String.valueOf(sum));


        }


    });


    mButtonAdd.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {


            AlertDialog alertDialog = new AlertDialog.Builder(BCdaily.this).create(); //Read Update

            alertDialog.setTitle("hi");


            alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    // here you can add functions

                }

            });


            alertDialog.show();  //<-- See This!

        }



    });


}

}


我尝试编写此代码(alertDialog.setMessage(mTextViewResult.setText(String.valueOf(sum));)但没有成功。


鸿蒙传说
浏览 149回答 2
2回答

qq_花开花谢_0

如果我理解你的实际问题,那么只需添加一键监听器,如下所示:&nbsp; mButtonAdd.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mEditText1.getText().toString().length() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mEditText1.setText("0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num1 = Integer.parseInt(mEditText1.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int sum = num1 * a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sumValue = String.valueOf(sum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextViewResult.setText(sumValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertDialog alertDialog = new AlertDialog.Builder(BCdaily.this).create(); //Read Update&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.setTitle("hi");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.setMessage(sumValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here you can add functions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertDialog.show();&nbsp; //<-- See This!&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

绝地无双

你必须改变alertDialog.setMessage(mTextViewResult.setText(String.valueOf(sum));到mTextViewResult.setText(String.valueOf(sum); alertDialog.setMessage.setText(mTextViewResult.getText().toString());另一件事是您正在使用相同的两个侦听器Button。用同样的方法做。您首先使用 inonClick()来设置文本TextView,然后使用第二个onCLick()来显示alertDialog。但你必须以一种Onclick方法而不是两种方法来使用这两种东西。因为你有一个button
随时随地看视频慕课网APP

相关分类

Java
我要回答