如何设置一个永远不可点击的按钮

时间:2018-03-30 12:39:00

标签: android firebase button firebase-realtime-database

在我的应用程序中,我有一个预订系统,允许用户在白天的特定时间预订开球时间。预订完成后,详细信息将保存到我的Firebase,然后用户可以关闭警报对话框。然后,当关闭警报对话框时,单击的按钮将变为不可用。问题在于,当用户离开预订活动并返回时,按钮可用,并且如果不同的用户然后访问该页面,则也可以点击该按钮。 我该如何解决这个问题? 我应该在9am孩子中保存用户的UID吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_booking);
    findViewById(R.id.profilebtn).setOnClickListener(this);
    findViewById(R.id.booking9am).setOnClickListener(this);
    book9am = (Button)findViewById(R.id.booking9am);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.profilebtn:
            finish();
            startActivity(new Intent(Booking.this, ProfileActivity.class));
            break;
        case R.id.booking9am:
            final AlertDialog.Builder mBuilder = new AlertDialog.Builder(Booking.this);
            View mView = getLayoutInflater().inflate(R.layout.dialog_booking,null);
            final EditText mPlayer1 = (EditText) mView.findViewById(R.id.player1);
            final EditText mPlayer2= (EditText) mView.findViewById(R.id.player2);
            final EditText mPlayer3 = (EditText) mView.findViewById(R.id.player3);
            final EditText mPlayer4 = (EditText) mView.findViewById(R.id.player4);
            final EditText mTime = (EditText) mView.findViewById(R.id.timeedit);
            final Button mBookingbtn = (Button) mView.findViewById(R.id.bookingbtn);
            mBookingbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String player1= mPlayer1.getText().toString().trim();
                    String player2= mPlayer2.getText().toString().trim();
                    String player4= mPlayer4.getText().toString().trim();
                    String player3= mPlayer3.getText().toString().trim();
                    if (player1.isEmpty()) {
                        mPlayer1.setError("Please enter player 1");
                        mPlayer1.requestFocus();
                        return;
                    }
                    if (player2.isEmpty()) {
                        mPlayer2.setError("Please enter player 2");
                        mPlayer2.requestFocus();
                        return;
                    }
                    if (player3.isEmpty()) {
                        mPlayer3.setError("Please enter player 2");
                        mPlayer3.requestFocus();
                        return;
                    }if (player2.isEmpty()) {
                        mPlayer4.setError("Please enter player 2");
                        mPlayer4.requestFocus();
                        return;
                    }
                    String playerone = mPlayer1.getText().toString();
                    String playertwo = mPlayer2.getText().toString();
                    String playerthree = mPlayer3.getText().toString();
                    String playerfour = mPlayer4.getText().toString();
                    String teetime= mTime.getText().toString().trim();
                    DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Booking").child("9am");
                    Map newPost = new HashMap();
                    newPost.put("playerone",playerone);
                    newPost.put("playertwo",playertwo);
                    newPost.put("playerthree",playerthree);
                    newPost.put("playerfour",playerfour);
                    newPost.put("teetime",teetime);
                    current_user_db.setValue(newPost);
                    Toast.makeText(Booking.this, "Booking Confirmed", Toast.LENGTH_SHORT).show();
                    book9am.setClickable(false);


                }


            });
            mBuilder.setNeutralButton("Close ", new DialogInterface.OnClickListener() { // define the 'Cancel' button
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            mBuilder.setView(mView);
            AlertDialog dialog = mBuilder.create();
            dialog.show();


    }

}
}

5 个答案:

答案 0 :(得分:1)

在onCreate方法中 -

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Booking").child("9am");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists())
        {
           book9am.setClickable(false);
        }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

答案 1 :(得分:0)

这个问题可能存在多种方法。 一种是在本地存储中为每个用户设置一个布尔变量共享首选项.... 点击按钮后,将值设置为true,当你回到应用程序时检查变量是否为true,然后禁用按钮..

第二个解决方案将varible存储在firebase上的每个用户并检查(建议用户可以更改电话)

答案 2 :(得分:0)

在显示活动之前,您必须向您的火力基地发出请求,以检查预订是否已完成,并根据结果使按钮启用或不启用。

答案 3 :(得分:0)

根据您的需要,您可以通过两种方法解决问题。

首先,正在保存按钮的状态本地(在客户端),这意味着在删除并重新安装应用程序之后,状态将是也重置。

为了永久保存按钮的状态,您应该在设备上保存所需的状态,这就是SharedPreferences的用途。 This是使用它的一个很好的例子。

以下是您应该如何在代码中实现它:

public static void set_isButtonClickable(Context ctx, Boolean bool) {
    SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
    editor.putBoolean("BUTTON_CLICKABLE_STATE", bool);
    editor.commit();
}

public static boolean getPrefIsFirstLaunch(Context ctx) {
    return getSharedPreferences(ctx).getBoolean("BUTTON_CLICKABLE_STATE",false);
}

其次,正在保存服务器端的按钮状态。删除并重新安装应用程序显然不会改变其状态。让每个用户都成为一个名为“button_state”的变量,并根据需要进行更改:

enter image description here

答案 4 :(得分:0)

findViewById(R.id.booking9am).setOnClickListener(this)而不是这个用途: -

    for ( ;; )
    {
        rc = ::readdir_r( ( DIR * ) dirp, dentbuffer, &dentp );
        if ( ( SUCCESS != rc ) || ( NULL == dentp ) )
        {
            return( NULL );
        }

        if ( dentp->d_type == DT_DIR )
        {
            break;
        }
    }

而不是 book9am.setClickable(false)设置 book9am.setEnable(false);

val headerSchema = List(
  StructField("example1", StringType, true),
  StructField("example2", StringType, true),
  StructField("example3", StringType, true)
)

val header_DF =sqlCtx.read
  .option("delimiter", ",")
  .option("header", "false")
  .option("mode","DROPMALFORMED")
  .option("inferSchema","false")
  .schema(StructType(headerSchema))
  .format("com.databricks.spark.csv")
  .load("folder containg the files")

如果你想在某些条件下禁用按钮,那么它也可以在服务器端进行管理。

相关问题