2014-01-19 58 views
8

我想这样做,只需点击一下按钮,就会弹出一条消息。带按钮的Android Eclipse弹出消息

现在弹出来,只要我打开应用程序。

BTW的按钮,我想触发弹出是main.xml中

关于按钮下面是我的main.xml(与布局的东西):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#3DE400" 
    android:orientation="vertical" > 

    <!-- background originally #d78a00 --> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="60dp" 
     android:fontFamily="sans-serif-condensed" 
     android:paddingLeft="10dp" 
     android:text="Sample App" 
     android:textColor="#FFF" 
     android:textSize="60sp" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fontFamily="sans-serif-condensed" 
     android:paddingLeft="10dp" 
     android:text="@string/creator" 
     android:textColor="#FFF" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/about" 
     android:layout_width="123dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:background="@android:color/transparent" 
     android:fontFamily="sans-serif-condensed" 
     android:gravity="left" 
     android:paddingLeft="10dp" 
     android:text="@string/about" 
     android:textColor="#FFF" 
     android:textSize="40dp" 
     android:onClick="show" /> 

</LinearLayout> 

这里是我的MainActivity。 Java的:

package com.pranavsanghvi.sampleappv4; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.view.Menu; 
import android.widget.Toast; 
import android.content.DialogInterface; 
import android.view.View; 
import android.widget.Button; 


public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
     alert.setTitle("About"); 
     alert.setMessage("Sample About"); 
     alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick (DialogInterface dialog, int id) { 
       Toast.makeText (MainActivity.this, "Success", Toast.LENGTH_SHORT) .show(); 
      } 
     }); 
     alert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT) .show(); 
      } 
     }); 
     alert.show(); 


    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

回答

0

如果你想显示关于按钮弹出单击添加onCreate()

以下3210
Button aboutButton = (Button) findViewById(R.id.about); 
aboutButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       alert.show(); 
      } 
     }); 

刚从onCreate()删除alert.show();;

更新: -

您是否获得警报不能得到解决?如果是的话要么使警报全球即外声明它onCreate()

public class MainActivity extends Activity { 
    AlertDialog.Builder alert; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
// code 
alert = new AlertDialog.Builder(MainActivity.this); 
// code 

或使其最终使其

final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 

也删除alert.show();这是onCreate();

+0

当我这样做,我有两个无法解决的警报和addNewContact ..我怎么能解决这个问题? – user3212831

+0

在'aboutbutton'' setOnClickListener'内只使用'alert.show()' –

+0

请参阅我的更新@ user3212831 –

0

首先,声明你的警报和MainActivity中的按钮:

public class Mainactivity extends Activity { 
    private AlertDialog.Builder alert; 
    private Button btAbout; 

    //rest of the code 
} 

然后,在的onCreate(),创建警报像你一样,除了这一行:

alert.show(); // <--- remove this line as not to show the alert immediately 

因为你已经宣布全球警报,记得也是在这里删除AlertDialog.Builder,所以不是:

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
alert.setTitle("About"); 

//rest of the code 

你应该有:

alert = new AlertDialog.Builder(MainActivity.this); 
alert.setTitle("About"); 

//rest of the code 

其次,获取句柄到你对按钮:

btAbout = (Button) findViewById(R.id.about); 

设置onClickListener到按钮:

btAbout.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     //when this button is clicked, show the alert 
     alert.show(); 
    } 
}); 

所有这一切都在的onCreate()。现在,当点击按钮时,您的警报将被显示。

+0

除了“alert.show();在“btAbout.setOnClickListener(新视图。OnClickListener(){“该警报有一个错误,指出”不能引用由另一个方法定义的内部类中的非最终变量警报“ – user3212831

+0

任何想法如何解决该问题? – user3212831

+0

您在MainActivity中声明了”alert“在onCreate()? – Melquiades