英文:
How to show the value of my tag when I click it?
问题
Sure, here is the translated code:
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
app:layout_constraintStart_toStartOf="parent"
<tag android:id="@+id/on_tag" android:value="1" />
</Button>
<Button
android:id="@+id/Off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:tag="0"
android:text="Apagar"
app:layout_constraintEnd_toStartOf="@+id/On"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="143dp"
<tag android:id="@+id/on_tag2" android:value="0" />
</Button>
Please note that the code you provided is a mixture of XML layout and Kotlin code. If you want to use these tags to retrieve values in your Kotlin code, you can refer to the following Kotlin code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.this)
val onButton = findViewById<Button>(R.id.On)
val offButton = findViewById<Button>(R.id.Off)
onButton.setOnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag)?.toString()
)
}
offButton.setOnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag2)?.toString()
)
}
}
}
This Kotlin code sets up click listeners for the "On" and "Off" buttons and uses getTag
to retrieve the values associated with the tags on_tag
and on_tag2
when the buttons are clicked.
英文:
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
app:layout_constraintStart_toStartOf="parent"
<tag android:id="@+id/on_tag" android:value="1" />
<Button
android:id="@+id/Off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:tag="0"
android:text="Apagar"
app:layout_constraintEnd_toStartOf="@+id/On"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="143dp"
<tag android:id="@+id/on_tag2" android:value="0" />
How can I show the value of my tag when I click it? I want to create onClick event to see the value 0 or 1 "I want an INT value" not string
It's the same if I use
android:id="@+id/Off"
android:value="1"
or
<tag android:id="@+id/On" android:value="1" />
This is the .kt I'm using kotlin and need to show the int value of the tag
class MainActivity : AppCompatActivity() {
override fun onClick(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.this)
On.setOnClickListener(View.OnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag)?.toString()
)
})
Off.setOnClickListener(View.OnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag2)?.toString()
)
})
}
}
答案1
得分: 1
Inside onClick(View v)
you can call v.getTag()
as follows
Button btnOn = (Button)findViewById(R.id.On);
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG,String.valueOf(v.getTag()));
}
});
But for the above code to work, your XML should be as follows.
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
android:tag="1"
app:layout_constraintEnd_toStartOf="@+id/Off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
/>
I did some research and trial updating the answer based on the question and your XML - also correcting it.
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
app:layout_constraintEnd_toStartOf="@+id/Off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<tag android:id="@+id/on_tag" android:value="1" />
</Button>
Just a small change in getTag()
, instead of view.getTag()
, you will pass the ID as below.
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, String.valueOf(v.getTag(R.id.on_tag)));
}
});
In the updated answer, the tag is separate, and you can have multiple tag values assigned to a view. Sorry, I forgot about tags because I never used them much. I generally set tags to views during runtime only. But thanks to you and the documentation.
Kotlin code for the same is below:
On.setOnClickListener(View.OnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag)?.toString()
)
})
As I have used on_tag
as the id
for the tag. Also, what you need is a String, while getTag
returns an Object, so converting it to a string.
英文:
Inside onClick(View v)
you can call v.getTag()
as follows
Button btnOn = (Button)findViewById(R.id.On);
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG,String.valueOf(v.getTag()));
}
});
But for above code to work you xml should be as follows.
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
android:tag="1"
app:layout_constraintEnd_toStartOf="@+id/Off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
/>
I did some research and trial Updating answer based on question and your xml - Also correcting it
<Button
android:id="@+id/On"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
android:text="Prender"
app:layout_constraintEnd_toStartOf="@+id/Off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
<tag android:id="@+id/on_tag" android:value="1" />
</Button>
Just small change in getTag() instead view.getTag()
will pass id as below.
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG,String.valueOf(v.getTag(R.id.on_tag)));
}
});
> In updated answer i.e tag as separate, we can have multiple Tag value
> assigned to a view. Sorry I forgot about tag because never
> used much, I generally set tag to view during run time only. but thanks to you and
> document
Kotlin code for same as below
On.setOnClickListener(View.OnClickListener { v ->
Log.e(
FragmentActivity.TAG,
v.getTag(R.id.on_tag)?.toString()
)
})
As I have used on_tag
in id
for tag
. Also what you need is String, while getTag returns Object so converting it to string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论