Вы можете сделать это следующим образом: 1) Иметь HashMap, где вы связываете обе ваши кнопки в одной строке друг с другом. 2) Иметь ArrayList идентификаторов кнопок, где вы можете хранить порядок нажатий. 3) Реализуйте метод, который будет выполнять сопоставление и вызовите его в методе #onCreate вашей Деятельности. 4) Установите глобальный экземпляр слушателя на все ваши кнопки.
private HashMap<Integer, Integer> buttonMap = new HashMap<>();
private ArrayList<Integer> buttonPressedOrder = new ArrayList<>();
// A global listener instance to be set to all of your buttons
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View selectedButton) {
int selectedButtonId = selectedButton.getId();
//Add pressed button to pressed buttons list
buttonPressedOrder.add(selectedButton.getId());
//Find button to hide and hide it
int hidingButtonId = buttonMap.get(selectedButtonId);
Button hidingButton = findViewById(hidingButtonId);
hidingButton.setVisibility(View.GONE);
}
}
//Put these inside your activity#onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mapButtons();
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
...
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
}
// A method for mapping your buttons in the same line to each other
private void mapButtons(){
buttonMap.put(R.id.button1, R.id.button2)
buttonMap.put(R.id.button2, R.id.button1)
buttonMap.put(R.id.button3, R.id.button4)
buttonMap.put(R.id.button4, R.id.button3)
...
}
Всякий раз, когда вам нужно увидеть, в каком порядке нажимаются кнопки, используйте этот метод
public void getButtonPressedOrder(){
Resources res = getResources();
int numberOfPressedButtons = buttonPressedOrder.size();
for(int i=0; i<numberOfPressedButtons; i++){
Log.i("PressOrder", res.getResourceEntryName(buttonPressedOrder.get(i))
+ " is pressed at " + (i+1) + " order");
}
}
, который будет регистрировать что-то вроде:
I / PressOrder: кнопка 1 нажата в 1 порядке
I / PressOrder: кнопка 5 нажата в 2 порядке
I / PressOrder: кнопка 10 нажата в 3 порядке
blockquote>Надеюсь, что это помогает!
Would TimeSpan.ToString() do the trick for you? If not, it looks like the code sample on that page describes how to do custom formatting of a TimeSpan
object.
Use String.Format() with multiple parameters.
using System;
namespace TimeSpanFormat
{
class Program
{
static void Main(string[] args)
{
TimeSpan dateDifference = new TimeSpan(0, 0, 6, 32, 445);
string formattedTimeSpan = string.Format("{0:D2} hrs, {1:D2} mins, {2:D2} secs", dateDifference.Hours, dateDifference.Minutes, dateDifference.Seconds);
Console.WriteLine(formattedTimeSpan);
}
}
}
The easiest way to format a TimeSpan
is to add it to a DateTime
and format that:
string formatted = (DateTime.Today + dateDifference).ToString("HH 'hrs' mm 'mins' ss 'secs'");
This works as long as the time difference is not more than 24 hours.
The Today
property returns a DateTime
value where the time component is zero, so the time component of the result is the TimeSpan
value.
According to the Microsoft documentation, the TimeSpan structure exposes Hours, Minutes, Seconds, and Milliseconds as integer members. Maybe you want something like:
dateDifference.Hours.ToString() + " hrs, " + dateDifference.Minutes.ToString() + " mins, " + dateDifference.Seconds.ToString() + " secs"
Преобразовав его в дату и время, вы можете получить локализованные форматы:
new DateTime(timeSpan.Ticks).ToString("HH:mm");