我现在有这个代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_serviciosp);
final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}
});
CustomList adapter = new CustomList(Agro.this, web, desc, tel, imageId, imageId3);
list = (ListView) findViewById(R.id.lvlista);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Agro.this, "Usted Clico en " + web[+position], Toast.LENGTH_SHORT).show();
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:22222222222222"));
startActivity(callIntent);
}
}
});
}
它的作用是在我的列表视图上方显示一个按钮,当我按下它显示弹出窗口的按钮时,但我想要做的是当我等待加入列表后3或4秒时它自动弹出而没有按下按钮。
此外,我想知道如何随机化自动弹出窗口,所以它最终会弹出我们,而不是每次我进入布局。
感谢
答案 0 :(得分:0)
要在4秒后打开弹出窗口,您可以在onCreate中使用以下代码
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnOpenPopup.performClick();
}, 4000);
它的作用是在开始活动4秒后模拟按钮点击。
如果你不想自动弹出用户已点击按钮,你可以拥有一个全局布尔值 - userClicked。如果用户点击将布尔值设置为true,否则最初将其设为false。在这种情况下,上述方法可以改为
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(!userClicked)
btnOpenPopup.performClick();
}, 4000);
对于随机化,只需使用随机生成器并获得1到5之间的int。如果int为1则显示,否则不要。你可以这样做。
Random random = new Random();
int x= random.nextInt(5)+1;
在处理程序方法中,你的if语句应该改为
if(!userClicked && x==1)
btnOpenPopup.performClick();
所以你的代码会改变为
prvate boolean userClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_serviciosp);
final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
userClicked=true;
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Random random = new Random();
int x= random.nextInt(5)+1;
if(!userClicked && x==1)
btnOpenPopup.performClick();
}}, 4000);
........