您好,我创建了一个测验应用程序,第二个Java类(Menu2)上有一个Button,如果有人单击此按钮,则会出现一个弹出窗口,并且背景(Menu2的Layout)的alpha /透明度为0.1。当有人在弹出窗口中单击时,弹出窗口会自行关闭,透明度将设置为1。但是,如果有人在弹出窗口之外单击,弹出窗口会关闭,但透明效果会持续...如何解决此问题?
菜单2:
public class Menu2 extends AppCompatActivity {
private DrawerLayout mDrawerLayout2;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private Button popup;
private PopupWindow popupWindow;private LayoutInflater layoutInflater; //Alows to add a new layout in our window
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);
TextView txtScore = (TextView) findViewById(R.id.textScore2);
TextView txtHighScore = (TextView)findViewById(R.id.textHighScore);
ImageView imgTrophyView1 = (ImageView)findViewById(R.id.trophy1);
ImageView imgTrophyView2 = (ImageView) findViewById(R.id.trophy2);
TextView trophy1Txt = (TextView) findViewById(R.id.trophy1Text);
TextView trophy2Txt = (TextView) findViewById(R.id.trophy2Text);
Intent intent = getIntent();
int mScore = intent.getIntExtra ("score",0);
txtScore.setText("Your score is: " + mScore);
SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
int applyView =sharedpreferences.getInt("currentscore",0);
SharedPreferences mypref =getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);
if (highScore>= mScore)
txtHighScore.setText("High score: " + highScore);
else{
txtHighScore.setText("New highscore: " + mScore);
SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore",mScore);
editor.commit();
}
if (applyView >=10) {
imgTrophyView1.setVisibility(View.VISIBLE);
trophy1Txt.setVisibility(View.VISIBLE);
}
if (applyView >= 20){
imgTrophyView2.setVisibility(View.VISIBLE);
trophy2Txt.setVisibility(View.VISIBLE);
}
popup = (Button)findViewById(R.id.enablePOPUP);
popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layoutInflater =(LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.popup_menu2_1,null);
popupWindow = new PopupWindow(container,1000,980,true); //400,400=popUp size, true = makes that we can close the pop up by simply click out of the window
popupWindow.showAtLocation(mDrawerLayout2, Gravity.CENTER, 0, 0);
mDrawerLayout2.setAlpha((float) 0.1);
container.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent ){
mDrawerLayout2.setAlpha((float) 1);
popupWindow.dismiss();
return true;
}
});
}
});
mToolbar = (Toolbar)findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout2 = (DrawerLayout) findViewById(R.id.drawerLayout2);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout2, R.string.open, R.string.close);
mDrawerLayout2.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView mNavigationView = (NavigationView) findViewById(nv2);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem){
switch (menuItem.getItemId()){
case(R.id.nav_home2):
Intent accountActivity2 = new Intent(getApplicationContext(),QuizActivity.class);
startActivity(accountActivity2);
}
return true;
}
});}
public void onClick(View view) {
Intent intent = new Intent(Menu2.this, QuizActivity.class);
startActivity(intent);
}
@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="amapps.impossiblequiz.Menu2"
android:id="@+id/drawerLayout2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/navigation_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/trophy2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 points= High knowledge"
android:visibility="invisible"
android:layout_alignTop="@+id/trophy1Text"
android:layout_alignLeft="@+id/trophy2"
android:layout_alignStart="@+id/trophy2"
android:layout_alignRight="@+id/trophy2"
android:layout_alignEnd="@+id/trophy2" />
<TextView
android:id="@+id/textScore2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#f60"
android:paddingTop="10dp"
android:paddingLeft="35dp"
android:text="Your score is:"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginTop="28dp"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textHighScore"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#FFAB00"
android:paddingLeft="35dp"
android:paddingRight="100dp"
android:paddingTop="10dp"
android:text="Highest score:"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginTop="13dp"
android:layout_below="@+id/textScore2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/tryAgain_button"
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/bg_pill"
android:onClick="onClick"
android:text="Restart Quiz!"
android:textColor="#ffffff"
android:layout_marginTop="18dp"
android:layout_below="@+id/textHighScore"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:text="Stats: play and get trophies!"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:id="@+id/trophy1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/tryAgain_button"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="40dp"
android:visibility="invisible"
app:srcCompat="@drawable/trophy1" />
<ImageView
android:id="@+id/trophy2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignTop="@+id/trophy1"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_toEndOf="@+id/trophy1"
android:layout_toRightOf="@+id/trophy1"
android:visibility="invisible"
app:srcCompat="@drawable/trophy2" />
<TextView
android:id="@+id/trophy1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10 points= Basic intelligence"
android:visibility="invisible"
android:layout_below="@+id/trophy1"
android:layout_alignLeft="@+id/trophy1"
android:layout_alignStart="@+id/trophy1"
android:layout_marginTop="23dp"
android:layout_toLeftOf="@+id/trophy2"
android:layout_toStartOf="@+id/trophy2" />
<ImageView
android:id="@+id/statsTrophy"
app:srcCompat="@drawable/statsoftrophy"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/textHighScore"
android:layout_above="@+id/tryAgain_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/enablePOPUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_below="@+id/trophy2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu2"
android:layout_gravity="start"
android:id="@+id/nv2"
app:headerLayout="@layout/navigation_header"
app:itemIconTint="@drawable/tint_color_selector2">
</android.support.design.widget.NavigationView>
最佳答案
尝试这样做:
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mDrawerLayout2.setAlpha((float) 1);
popup.dismiss();
}
});