2017-08-04 114 views
1

當前我正在使用以下代碼並創建自定義對話框。並點擊按鈕時,對話框打開,但我想通過點擊OK按鈕隱藏對話框..但它不工作,並沒有顯示錯誤... 我用'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(); 
} 
}); 
} 
}); 

請人幫我隱藏按鈕click..thanks

+0

試試這個被接受的答案https://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android –

回答

0

這是編寫自定義對話框有道對話框。

//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(); 
    } 
});