一个按钮用于2种不同的动作

时间:2017-08-20 17:39:14

标签: java android

我有"发送" dialogFragment中的按钮,onClick事件将新数据按键值推送到firebase。

我希望这个按钮也像一个"更新"用户单击特定按钮时的按钮。数据将在firebase中以与之前相同的键值更新。

这是onClick方法的发送按钮:

Route::get('image/{path}', function(){
    return Storage::get($path);
})->name('image');

//acess the image
<img src="{{ route('image', $spreference->favicon) }}">

有什么建议吗?

4 个答案:

答案 0 :(得分:0)

这是一个例子:

private final int BUTTON_UPDATE = 1;
private final int BUTTON_SEND = 2;
private int buttonStatus = 0;

send_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          switch(buttonStatus)
          {
            case BUTTON_UPDATE: { // your update code here}
            case BUTTON_SEND : { // your send code here
        }

buttonStatus将控制按钮的操作。

答案 1 :(得分:0)

  1. 在sharedPrefrences中放置一个布尔值,并在初始时保持为false     用户将数据发送到firebase update boolean为true。

  2. onClick中的
  3. 使用if else检查值是true还是false 相应地执行代码

答案 2 :(得分:0)

比实施更重要的是你认为可以做到的方式。因此,这些情况下的基本方法是使用布尔变量。

为什么呢?因为它可用于指示按钮是否处于特定状态。 所以,你可以这样做。

boolean b=false;
//set your button in the initial state you want(submit in your case)
 //In onClick() method
if(!b){         //button in submit state
b=true;        
//do submit stuff
send_btn.setText("update");
}
else{         //button in update state
b=false;
//do update stuff
send_btn.setText("submit");
}

在这种情况下,b的真值表示按钮处于“更新”状态。

答案 3 :(得分:0)

您可以使用视图的标记来根据需要更新状态

  Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (button.getTag().equals("send")) {
                    // push the value to firebase

                    // set the tag to update and the text
                    button.setTag("update");
                    button.setText("update");
                }else if (button.getTag().equals("update")){
                    // update the value in firebase
                }
            }
        });

XML

<Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="send"
        android:text="send" />