我很新的Android。我想在我的應用程序的彈出窗口。我有一個TextView和我activity_main.xml中文件的按鈕,和一個TextView,並在popup.xml文件 兩個Button下面是這些文件彈出窗口不會出現,當我點擊
popup.xml file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is popoup" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/Unpair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unpair" />
<Button
android:id="@+id/Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel" />
</TableRow>
</TableLayout>
這裏是activity_main.xml中文件
activity_main.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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="18dp"
android:layout_marginTop="28dp"
android:layout_toRightOf="@+id/textView1"
android:text="Cick me" />
</RelativeLayout>
我想要顯示一個彈出窗口,當我點擊「點擊我」按鈕。但是,彈出窗口不會出現當我這樣做按下按鈕
The java code
package com.example.ctrckerapp;
import android.os.Bundle;
import android.widget.*;
import android.app.Activity;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity {
private PopupWindow pw;
private Button Cancel;
private Button Unpair;
private Button Click;
View layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Click=(Button)findViewById(R.id.btn1);
Click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
initiatePopup();
}
});
try{
Cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
pw.dismiss();
}
});
layout.post(new Runnable(){
public void run()
{
pw.showAtLocation(layout, Gravity.BOTTOM, 100, 700);
}
});
}catch(Exception e){
Toast.makeText(getApplicationContext(),
e.toString(),Toast.LENGTH_LONG).show();
}
}
public void initiatePopup()
{
try{
LayoutInflater inflater=
(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout=inflater.inflate(R.layout.popup,
(ViewGroup)findViewById(R.id.popup_element));
pw=new PopupWindow(layout,300,670,true);
Cancel =(Button)layout.findViewById(R.id.Cancel);
Unpair=(Button)layout.findViewById(R.id.Unpair);
}catch(Exception e){
Toast.makeText(getApplicationContext(),
e.toString(),Toast.LENGTH_LONG).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;
}
}