如图所示,上面的部分是我的布局,我想在图像中添加如下部分的弹出窗口。所以如何创建它以及如何处理对话框按钮事件。??
答案 0 :(得分:4)
public class Add extends Activity{
PopupWindow popUp;
Dialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
myDialog = new Dialog(Add.this);
myDialog.setContentView(R.layout.popup50);
myDialog.setTitle("Please Pay Attention!");
myDialog.setCancelable(true);
//for save
Button popone = (Button)myDialog.findViewById(R.id.button1);
popone.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
myDialog.dismiss();
}
});
//for cancel
Button poptwo = (Button)myDialog.findViewById(R.id.button2);
poptwo.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent CheckProfile = new Intent(Add.this, UserActivity.class);
startActivity(CheckProfile);
myDialog.dismiss();
}
});
myDialog.show();
}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#00FFFF"
android:layout_width="300dp"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/popUp_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:textColor="#000000"
android:text="@string/pop50"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/popUp_text"
android:layout_marginRight="36dp"
android:layout_marginTop="44dp"
android:text="Cancel" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginRight="69dp"
android:layout_toLeftOf="@+id/button2"
android:text=" Ok " />
</RelativeLayout>
答案 1 :(得分:2)
我有类似的东西显示一个可能对您有帮助的屏幕:
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
show_about();
}
});
...
private void showAbout() {
Dialog dialog = new Dialog (context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.about);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}
xml位:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="An app for the Newcastle Cloud Team"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView2"
android:layout_centerHorizontal="true"
android:src="@drawable/coche3" />
...
</RelativeLayout>
答案 2 :(得分:1)
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/image"/>/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text=" Ok " />
将此代码放入按钮事件或任何您想要的内容
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.coustom);
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 ");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
答案 3 :(得分:1)
public class PopupforeditActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabetslist);
callalertDialogueCustom();
}
public void callalertDialogueCustom(){
final Dialog dialog = new Dialog(PopupforeditActivity.this);
dialog.setContentView(R.layout.popup); // This is my Custom Layout
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.txt1);
text.setText("Lots of Hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
EditText edt=(EditText) dialog.findViewById(R.id.edtxtName);
edt.setText("i am binded this time");
//set up image view
/* ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.nista_logo);*/
//set up button
Button button = (Button) dialog.findViewById(R.id.btnSave);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You clicked on Save",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
Button button1 = (Button) dialog.findViewById(R.id.btnCancel);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//finish();
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}
我的自定义布局GUI
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rltvview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_black_transparent">
<ScrollView
android:id="@+id/scrlview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_centerInParent="true"
android:background="@android:color/darker_gray" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:layout_marginTop="15dip"
android:text="Name"
android:textColor="@android:color/black"
android:background="@drawable/btn_orange_matte"/>
<EditText
android:id="@+id/edtxtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:scrollbars="vertical"
android:hint="hii"
android:lines="5"
android:singleLine="false"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Save"
android:layout_margin="10dip"/>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Cancel"
android:layout_margin="10dip"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
只需根据您的需要从任何地方调用 callalertDialogueCustom(); 方法。它也会创建自定义对话,并且还会为它处理点击事件
答案 4 :(得分:0)
答案 5 :(得分:0)
阅读此link以获取对话框示例,它与您在上面的图片中提到的相同:)