我有一个操作栏,其中包含"关于"和#34;一个图像"和"设置"当"关于被触及"自定义对话框按钮打开,我在xml文件中使用了一个按钮" dialogButton"
基本上,我希望对话框在触及时解除。
Actionbar.java
:
package com.example.actionbar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
public class ActionBar extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.testmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.about:
aboutMenuItem();
break;
case R.id.settings:
settingsMenuItem();
break;
case R.id.item3:
item3MenuItem();
break;
}
return true;
}
private void aboutMenuItem(){
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog_custom);
d.setTitle("Custom Dialog");
d.findViewById(R.id.dialogButton);
d.show();
}
private void settingsMenuItem() { }
private void item3MenuItem() { }
}
和我的xml文件dialog_custom.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/dialog_webview"
android:layout_width="match_parent"
android:layout_height="68dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Dialog" />
<Button
android:id="@+id/dialogButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="OK" />
答案 0 :(得分:1)
试试这个
private void aboutMenuItem(){
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog_custom);
d.setTitle("Custom Dialog");
Button connect = (Button) d.findViewById(R.id.dialogButton);
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
答案 1 :(得分:1)
您可以在aboutMenuItem()函数中执行类似的操作。
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
View view = getLayoutInflater().inflate(R.layout.yourCustomDialogLayout,
null);
Button dialogButton = (Button) view.findViewById(R.id.dialogButton);
dialogButton .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setContentView(view);
dialog.setCancelable(false);
dialog.show();
答案 2 :(得分:0)
你可以试试这个..
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); // find button with dialog's preference..
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();