如何在Android中重置AtomicInteger唯一ID?

时间:2015-09-03 17:09:13

标签: java android uniqueidentifier

我想用重置按钮创建一个唯一的ID。我有一个班级ViewIdViewId班级包含AtomicIntegerAtomicInteger已成功创建唯一ID。但我想使用按钮重置唯一ID。请帮助我重置并再次创建唯一ID。

ViewId上课:

import java.util.concurrent.atomic.AtomicInteger;

public class ViewId {

private static ViewId INSTANCE = new ViewId();

private AtomicInteger seq;

private ViewId() {
    seq = new AtomicInteger(0);
}

public int getUniqueId() {
    return seq.incrementAndGet();
}

public static ViewId getInstance() {
    return INSTANCE;
}
}

MainActivity上课:

public class MainActivity extends AppCompatActivity {

EditText editValue;
ViewId viewId = ViewId.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editValue = (EditText)findViewById(R.id.editText);

}

// Increment Button
public void floatIncrement(View view) {
    Log.e("Incremented value is : ", String.valueOf(viewId.getUniqueId()));
}

// Reset Button
public void resetBtn(View view) {
    // reset here
}
}

1 个答案:

答案 0 :(得分:5)

只需拨打seq.set(0),即可将AtomicInteger恢复为0。