Android 尝试生成一个简单的弹出窗口
我试图在点击按钮时生成一个简单的弹出窗口,但由于某些原因,它不起作用。下面是我的代码:Android 尝试生成一个简单的弹出窗口,android,android-xml,Android,Android Xml,我试图在点击按钮时生成一个简单的弹出窗口,但由于某些原因,它不起作用。下面是我的代码: public class Product extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);
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" >
您需要调用
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
}
我不知道人们怎么能忘记它,但你能描述一下发生了什么吗?我相信你可以比“它不起作用”做得更好。你做了什么,你期望什么,你为什么期望这个,实际发生了什么,有没有任何错误,你自己做过任何调试/跟踪,你所有变量的状态是什么,所有函数都被触发了。老实说,如果你自己似乎没有兴趣,你怎么能期望一个人对这个问题感兴趣呢?这就是让我吃惊的地方,我没有记录猫的错误!我有正常的程序执行,当我点击按钮时,什么都没有发生,这里询问这个问题的唯一原因是收集专家建议我失败的地方。是的,如果我在所说的onClickListener中添加一些其他功能,它会起作用,就像试图打开一个新的意图。然后实际添加它!“我按下按钮,但什么也没发生。我已经检查了日志……等等。”。我们猜不到这些东西!然后,向onclick函数添加一些日志记录,以显示它实际上已被触发。并解释您希望在代码中发生什么,并检查它是否发生/为什么发生。等
</ScrollView>
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
}