试图生成一个简单的弹出窗口

时间:2013-01-10 08:16:54

标签: android android-xml

我试图在点击按钮时生成一个简单的弹出窗口,但由于某种原因它无法正常工作。以下是我的代码:

     public class Product extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.product);
    String fontPath = "fonts/georgia.ttf";
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

    final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);


    Button Particle =(Button)findViewById(R.id.button1); 
    Button MDF=(Button)findViewById(R.id.button2);
    Button Laminates=(Button)findViewById(R.id.button3);
    Button Ply =(Button)findViewById(R.id.button4); 
    Button Floor=(Button)findViewById(R.id.button5);
    Button Door=(Button)findViewById(R.id.button6);
    Button EdgeBand=(Button)findViewById(R.id.button7);
    Button ModFur=(Button)findViewById(R.id.button8);



    Particle.setTypeface(tf);
    MDF.setTypeface(tf);
    Laminates.setTypeface(tf);
    Ply.setTypeface(tf);
    Floor.setTypeface(tf);
    Door.setTypeface(tf);
    EdgeBand.setTypeface(tf);
    ModFur.setTypeface(tf);

   /* Particle.startAnimation(animScale);
    MDF.startAnimation(animScale);
    Laminates.startAnimation(animScale);
    Ply.startAnimation(animScale);
    Floor.startAnimation(animScale);
    Door.startAnimation(animScale);
    EdgeBand.startAnimation(animScale);
    ModFur.startAnimation(animScale); */


    Particle.setOnClickListener(Par);

}


private View.OnClickListener Par = new View.OnClickListener(){
    public void onClick(View v){

        openNewDialog();


    }
};



private void openNewDialog() {
    new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialoginterface,int i){
        if(i==0){
            Intent about = new Intent(Product.this,AboutUs.class);
            startActivity(about);
        }
        if(i==1){
            Intent about = new Intent(Product.this,AboutUs.class);
            startActivity(about);
        }

    }}); 

}

在Strings.xml文件中:

     <array name="pop">
      <item name="easy_label">Key Features</item>
      <item name="medium_label">Advantages</item> 
     </array>

xml中所述活动的布局文件:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/universalbg"
android:orientation="vertical" >
 <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="90dp"
       android:text="@string/btn2"
       android:textColor="#FFFFFF"
       android:textSize="24dp"
       android:textStyle="bold"  />
        </RelativeLayout>

<ScrollView 
     android:layout_marginTop="10dip" 
     android:layout_marginBottom="03dip" 
     android:id="@+id/Scroll" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" >   

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn2"
    android:textColor="#900606" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn3"
    android:textColor="#900606" />


 <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_below="@+id/button3"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn4"
    android:textColor="#900606" />

 <Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button4"
    android:layout_below="@+id/button4"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn5"
    android:textColor="#900606" />

  <Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button5"
    android:layout_below="@+id/button5"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn6"
    android:textColor="#900606" />

  <Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button6"
    android:layout_below="@+id/button6"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn7"
    android:textColor="#900606" />

   <Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button7"
    android:layout_below="@+id/button7"
    android:layout_marginTop="20dp"
    android:background="@drawable/insidebut"
    android:text="@string/BTn8"
    android:textColor="#900606" />

</ScrollView>

1 个答案:

答案 0 :(得分:5)

您需要致电show()才能真正显示对话框。

private void openNewDialog() {
    new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialoginterface,int i){
        if(i==0){
            Intent about = new Intent(Product.this,AboutUs.class);
            startActivity(about);
        }
        if(i==1){
            Intent about = new Intent(Product.this,AboutUs.class);
            startActivity(about);
        }

    }}).show(); // Here

}