如何在对话框功能中对齐消息并重用该功能?
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)
没有任何效果。有人可以帮我解决这个问题吗?
从这个网站找到了我的问题的解决方案:http://examples.javacodegeeks.com/android/core/ui/dialog/android-custom-dialog-example/
按照网站上的描述创建了一个 xml 布局,并对我 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();
}
然后我通过更改消息多次重复使用此函数:
alertView("Please select one of the red icons to begin");
我想为对话框方法创建一个函数并在以后重用该函数。
在函数内创建对话框的代码:
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)
没有任何效果。有人可以帮我解决这个问题吗?
从这个网站找到了我的问题的解决方案:http://examples.javacodegeeks.com/android/core/ui/dialog/android-custom-dialog-example/
按照网站上的描述创建了一个 xml 布局,并对我 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();
}
然后我通过更改消息多次重复使用此函数:
alertView("Please select one of the red icons to begin");