我想用重置按钮创建一个唯一的ID。我有一个班级ViewId
。ViewId
班级包含AtomicInteger
。 AtomicInteger
已成功创建唯一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
}
}
答案 0 :(得分:5)
只需拨打seq.set(0)
,即可将AtomicInteger
恢复为0。