gpt4 book ai didi

java - 单击按钮时隐藏自定义对话框

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:21 26 4
gpt4 key购买 nike

目前我正在使用以下代码并创建一个自定义对话框。单击按钮时会打开对话框,但我想通过点击“确定”按钮来隐藏对话框..但它不起作用并且不显示错误...我使用了“dialog.dissmis”代码在这里:

final Dialog dialog =new Dialog(ActionBarActivity. this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_dictionary);
TextView word = (TextView)dialog.findViewById(R.id.txtWord);
final TextView wordMeaning = (TextView)dialog.findViewById(R.id.txtMeaning);
dialog.getWindow().getAttributes().windowAnimations = R.style.AnimLeftRight;

//Get Words and it's meanings.

word.setText(Dictionary.getWord());

wordMeaning.setText(Dictionary.getMeaning());

Button btntts = (Button)dialog.findViewById(R.id.btntts);
final Button ok = (Button)dialog.findViewById(R.id.btnPositive);

// Show the dialog first.
dialog.show();


WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;

dialog.getWindow().setAttributes(lp);


btntts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String convertTextToSpeech
wordMeaning.getText().toString();

convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

@Override
public void onInit(int status) {

if(status != TextToSpeech.ERROR){

convertToSpeech.setLanguage(Locale.US);

convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
});

ok.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

dialog.dismiss();
}
});
}
});

请任何人帮助我隐藏单击按钮时的对话框..谢谢

最佳答案

这是编写自定义对话框的正确方法。

//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.custom_alert_view, null);

//Set the layout inside of the builder
builder.setView(builderView);

//Show the dislog
final AlertDialog alert = builder.show();

//Get Button from the layout.
Button dismiss = (Button) builderView.findViewById(R.id.dismiss);


//Click the dismiss button from within the alert. will dismiss the dialog box
dismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});

关于java - 单击按钮时隐藏自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45511489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com