2014-10-31 86 views
1

我已經寫了一些行來彈出一個窗體,按Button。現在我試圖停用Button,如果它按下,所以它不會再次打開。禁用一個按鈕,如果它已被執行

這裏是塔的主要活動文件:

package com.javacodegeeks.android.fragmentstest; 

import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.PopupWindow; 
import android.widget.Toast; 
import android.annotation.SuppressLint; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.Context; 
import android.content.DialogInterface; 


@SuppressLint("InflateParams") 
public class MainActivity extends Activity implements OnClickListener { 

    ImageView mButton1; 
    Context contex; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     mButton1 = (ImageView) findViewById(R.id.button1); 
     mButton1.setOnClickListener(new OnClickListener() { 

      @SuppressWarnings("null") 
     public void onClick(View arg0) { 
      LayoutInflater layoutInflater 
      = (LayoutInflater)getBaseContext() 
       .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.popupform, null); 
        final PopupWindow popupWindow = new PopupWindow(
         popupView, 
         LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT); 

        Button btnbutton1 = (Button)popupView.findViewById(R.id.login); 

        btnbutton1.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       popupWindow.dismiss(); 
      }}); 


        popupWindow.showAsDropDown(btnbutton1, 50, 250); 

      } 
     }); 

    } 

我們做出說明在這裏,這是代碼的一部分,因此有在這之後更多。但我卡在這一部分!

回答

1

只是禁用它...

mButton1.setOnClickListener(new OnClickListener() { 

     @SuppressWarnings("null") 
    public void onClick(View arg0) { 
     mButton1.setEnabled(false); 
     // your other code 

你也可以只設置在View,如果你在未來如果你想重新使用此代碼對其他Views

public void onClick(View arg0) { 
     arg0.setEnabled(false); 
     // your other code 

啓用它然後簡單地覆蓋彈出的onDismiss()並啓用Button

@Override 
public void onDismiss() { 
    mButton1 .setEnabled(true); 

Docs for setEnabled()

Docs for onDismiss()

我也會考慮改變參數去有意義的事。而不是默認arg0使用類似vview

View popupView = layoutInflater.inflate(R.layout.popupform, null); 
       final PopupWindow popupWindow = new PopupWindow(
        popupView, 
        LayoutParams.WRAP_CONTENT, 
         LayoutParams.WRAP_CONTENT); 
popupWindow.setOnDismissListener(new PopupWindow.DismissListener 
{ 
    @Overide 
    public void onDismiss() { 
     mButton1 .setEnabled(true); 
    } 
}); 
+0

如果我想關閉該彈出窗口後啓用它呢? – 2014-10-31 20:31:29

+0

這就是爲什麼我添加了關於重寫onDismiss()的一點。這將被稱爲當你關閉彈出 – codeMagic 2014-10-31 20:39:41

+0

年,只是顯示這一點。那麼這應該在哪裏? – 2014-10-31 21:16:04

0

你可以簡單地做button.setEnabled(false);來禁用按鈕。你應該嘗試這種方法。爲表單打開時創建一個布爾值,如果爲true,則禁用該按鈕。

相關問題