2013-01-10 50 views
-1

我試圖生成一個簡單的彈出按鈕,但由於某種原因它不起作用。下面是我的代碼:試圖生成一個簡單的彈出框

 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> 

+0

我不確定一個人怎麼可以忘記它,但你能真正描述會發生什麼嗎?我相信你可以做得更好,然後「它不工作」。你做什麼,你期望什麼,你爲什麼期望這樣,實際發生了什麼,是否有任何錯誤,你是否做過任何調試/跟蹤,所有變量的狀態是什麼,是否觸發了所有的功能。老實說,如果你自己似乎沒有這個問題,你如何期待對這個問題有興趣? – Nanne

+0

這就是驚人的我,我沒有登錄錯誤!我有正常的程序執行,當我點擊按鈕什麼都沒有發生時,在這裏問這個問題的唯一理由是收集專家建議我失敗的地方。 – Skynet

+0

是的,如果我添加一些其他功能的onClickListener,它的作品,就像試圖打開一個新的意圖。 – Skynet

回答

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 

} 
+1

非常感謝你:) – Skynet