80,350
社区成员
发帖
与我相关
我的任务
分享
package com.example.zhuwen.model_popubwindow;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.EditText;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity{
private PopupWindow popupWindow;
private View contentView;
private TextView tx_pop;
private TextView tx_test;
private EditText ed_test;
int width=0,height=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx_pop = (TextView)findViewById(R.id.tx_pop);
tx_test = (TextView)findViewById(R.id.tx_test);
ed_test = (EditText)findViewById(R.id.ed_test);
//get the width and height of screen
final DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
width = metric.widthPixels/2;
height = metric.heightPixels/2;
tx_test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Can Touch",
Toast.LENGTH_SHORT).show();
}
});
tx_pop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
get_pop();
}
});
}
public void get_pop() {
contentView = getLayoutInflater().inflate(R.layout.pop_win, null);
popupWindow = new PopupWindow(contentView, 300,
200);
popupWindow.setContentView(contentView);
//.setBackgroundDrawable(getResources().getDrawable(R.drawable.search_label_click));
//.setBackgroundColor(Color.parseColor("#F5F5DC"));
//.setBackgroundColor(Color.GRAY);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.test));
//set "true" ,then you can enter something
//set "true",a popwin will disappear when you touch the outside of the popwin
popupWindow.setFocusable(true);
//decided the location of a popwin
popupWindow.showAtLocation(R.id.tx_pop,Gravity.LEFT, 0, 0);
//the command to close a popwin
//popupWindow.dismiss();
}
}