可以使用xml属性onClick=”click”来更改按钮文本,而无需创建Button对象。

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

Is possible to change Button text using xml attribute onClick="click" and without creating a Button Object

问题

我是Android应用开发的初学者,我看到有两种处理按钮点击的方法。
第一种是使用setOnClickListener,第二种是使用xml属性onClick
对于初学者/我来说,onClick xml属性非常简单。
我可以通过创建一个按钮对象然后使用以下方式更改按钮文本:btn.setText("按钮点击");
但是我的问题是,我是否可以在不创建按钮对象的情况下使用setTextisEnabled等方法来操作按钮。
是否可能在不创建按钮对象或不使用xml属性onClick的情况下更改按钮文本。

英文:

I am a beginner in android app development, I have seen there are two methods to handle on button click.
First one is using setOnClickListener and second is using xml attribute onClick.

onClick xml attribute quite easy for beginner/me.

I can change button text by creating an object of a button and then: btn.setText("Button Clicked");

But my question is can I use setText, isEnabled, etc.. methods for button without creating its object.

Is possible to change button text without creating a Button Object or using the XML attribute onClick.

答案1

得分: 2

  1. 在您的 build.gradle 文件中(模块:app),添加以下内容:
android {
    buildFeatures{
        dataBinding = true
    }
}
  1. 在您的字符串资源文件 (res/values/strings.xml) 中添加以下内容:
<resources>
    <string name="button_clicked">Button Clicked</string>
</resources>
  1. activity_main.java 中:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="com.example.myapplication.R"/>
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{() -> button.setText(R.string.button_clicked)}"/>

    </FrameLayout>
</layout>

这样可以使您在不需要按钮实例的情况下更改按钮文本。但我认为这不是一种最优方法,标准的 setOnClickListener 方法更好。


另一种方法

  1. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="buttonClicked"
            type="java.lang.String"
            />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{() -> button.setText(buttonClicked)}"/>

    </FrameLayout>
</layout>
  1. MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import com.example.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.setButtonClicked("Button clicked");
    }
}
英文:
  1. To Your build.gradle (Module: app) add this:
android {
    buildFeatures{
        dataBinding = true
    }
}
  1. To Your String resources (res/values/strings.xml) add this:
&lt;resources&gt;
    &lt;string name=&quot;button_clicked&quot;&gt;Button Clicked&lt;/string&gt;
&lt;/resources&gt;
  1. And in activity_main.java:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;data&gt;
        &lt;import type=&quot;com.example.myapplication.R&quot;/&gt;
    &lt;/data&gt;

    &lt;FrameLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;Button
            android:id=&quot;@+id/button&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:onClick=&quot;@{()-&gt;button.setText(R.string.button_clicked)}&quot;/&gt;

    &lt;/FrameLayout&gt;
&lt;/layout&gt;

This will allow You to change button text without having a button instance. But I think this is not an optimal method and the standard setOnClickListener is a way better.

<hr>

Another method

  1. activity_main.xml:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;data&gt;
        &lt;variable
            name=&quot;buttonClicked&quot;
            type=&quot;java.lang.String&quot;
            /&gt;
    &lt;/data&gt;

    &lt;FrameLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;Button
            android:id=&quot;@+id/button&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:onClick=&quot;@{()-&gt; button.setText(buttonClicked)}&quot;/&gt;
        
    &lt;/FrameLayout&gt;
&lt;/layout&gt;
  1. MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import com.example.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.setButtonClicked(&quot;Button clicked&quot;);
    }
}

答案2

得分: 1

这可以通过在onClick处理程序中进行操作来实现。

或者

可以使用数据绑定来完成:只需将您的片段实例添加为变量,然后您就可以将任何方法与onClick链接。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.testapp.fragments.CustomFragment">

<data>
    <variable android:name="fragment" android:type="com.example.testapp.fragments.CustomFragment"/>
</data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_place_black_24dp"
        android:onClick="@{() -> fragment.buttonClicked()}"/>
</LinearLayout>
</layout>
英文:

This can be achieved by doing manipulation in onClick handlers.

or

It can be done with data bindings: Just add your fragment instance as a variable, then you can link any method with onClick.

&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
tools:context=&quot;com.example.testapp.fragments.CustomFragment&quot;&gt;

&lt;data&gt;
    &lt;variable android:name=&quot;fragment&quot; android:type=&quot;com.example.testapp.fragments.CustomFragment&quot;/&gt;
&lt;/data&gt;
&lt;LinearLayout
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;ImageButton
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:src=&quot;@drawable/ic_place_black_24dp&quot;
        android:onClick=&quot;@{() -&gt; fragment.buttonClicked()}&quot;/&gt;
&lt;/LinearLayout&gt;

</layout>

huangapple
  • 本文由 发表于 2020年9月11日 15:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63842991.html
匿名

发表评论

匿名网友

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

确定