当我点击标签时,如何显示我的标签的值?

huangapple go评论90阅读模式
英文:

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.

英文:
 &lt;Button
android:id=&quot;@+id/On&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;8dp&quot;
android:layout_marginTop=&quot;92dp&quot;
android:text=&quot;Prender&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
&lt;tag android:id=&quot;@+id/on_tag&quot; android:value=&quot;1&quot; /&gt;


&lt;Button
    android:id=&quot;@+id/Off&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;8dp&quot;
    android:tag=&quot;0&quot;
    android:text=&quot;Apagar&quot;
    app:layout_constraintEnd_toStartOf=&quot;@+id/On&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    tools:layout_editor_absoluteY=&quot;143dp&quot;
    &lt;tag android:id=&quot;@+id/on_tag2&quot; android:value=&quot;0&quot; /&gt;

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=&quot;@+id/Off&quot;
android:value=&quot;1&quot;

or

&lt;tag android:id=&quot;@+id/On&quot; android:value=&quot;1&quot; /&gt;

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 -&gt;
            Log.e(
                FragmentActivity.TAG,
                v.getTag(R.id.on_tag)?.toString()
            )
        })

        Off.setOnClickListener(View.OnClickListener { v -&gt;
            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.

&lt;Button
    android:id=&quot;@+id/On&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;8dp&quot;
    android:layout_marginTop=&quot;92dp&quot;
    android:text=&quot;Prender&quot;
    android:tag=&quot;1&quot;
    app:layout_constraintEnd_toStartOf=&quot;@+id/Off&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;
/&gt;

I did some research and trial Updating answer based on question and your xml - Also correcting it

&lt;Button
    android:id=&quot;@+id/On&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;8dp&quot;
    android:layout_marginTop=&quot;92dp&quot;
    android:text=&quot;Prender&quot;
    app:layout_constraintEnd_toStartOf=&quot;@+id/Off&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;
&lt;tag android:id=&quot;@+id/on_tag&quot; android:value=&quot;1&quot; /&gt;
&lt;/Button&gt;

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 -&gt;
            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.

huangapple
  • 本文由 发表于 2020年1月3日 16:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定