如何在对话框功能中对齐消息并重用该功能?(How to align message in dialog function and reuse the function?)

我想为对话框方法创建一个函数,并在以后重用该函数。

用于在函数内创建对话框的代码:

private void alertView( String message ) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle( "Hello" ) .setIcon(R.drawable.ic_launcher) .setMessage(message) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i){ } }).show(); }

调用此函数的代码:

alertView("My message");

这工作正常,但我想集中我的信息。 我寻找解决方案并使用各种方法,例如:

AlertDialog alert = dialog.show(); TextView messageText =(TextView)alert.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER); messageText.setTextColor(Color.RED)

什么都行不通。 有人可以帮我这个吗?

I want to create a function for dialog method and reuse the function later on.

Code to create a dialog within a function:

private void alertView( String message ) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle( "Hello" ) .setIcon(R.drawable.ic_launcher) .setMessage(message) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i){ } }).show(); }

Code to call this function:

alertView("My message");

This works fine but I want to center my message. I have looked for solutions and used various methods such as:

AlertDialog alert = dialog.show(); TextView messageText =(TextView)alert.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER); messageText.setTextColor(Color.RED)

Nothing works. Could someone please help me with this?

最满意答案

从这个网站找到了我的问题的解决方案: http : //examples.javacodegeeks.com/android/core/ui/dialog/android-custom-dialog-example/

创建了一个xml布局,如网站上所述,并对我的java类中的代码进行了一些更改:

private void alertView( String message ) { //create a dialog component final Dialog dialog = new Dialog(this); //tell the dialog to use the dialog.xml as its layout description dialog.setContentView(R.layout.dialog); dialog.setTitle("your title"); TextView txt = (TextView) dialog.findViewById(R.id.txt); txt.setText(message); Button dialogButton = (Button)dialog.findViewById(R.id.dialogButton); dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mUartCom.write("D"); //change this } }); dialog.show(); }

然后我通过更改消息多次重复使用此功能:

alertView("Please select one of the red icons to begin");

Found a solution to my question from this website: http://examples.javacodegeeks.com/android/core/ui/dialog/android-custom-dialog-example/

Created an xml layout as described on the website and made a little change to the code in my java class:

private void alertView( String message ) { //create a dialog component final Dialog dialog = new Dialog(this); //tell the dialog to use the dialog.xml as its layout description dialog.setContentView(R.layout.dialog); dialog.setTitle("your title"); TextView txt = (TextView) dialog.findViewById(R.id.txt); txt.setText(message); Button dialogButton = (Button)dialog.findViewById(R.id.dialogButton); dialogButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mUartCom.write("D"); //change this } }); dialog.show(); }

and then I've reused this function numerous times by changing the message:

alertView("Please select one of the red icons to begin");

更多推荐