【问题标题】:Is possible to change Button text using xml attribute onClick="click" and without creating a Button Object可以使用 xml 属性 onClick="click" 更改按钮文本,而无需创建按钮对象
【发布时间】:2020-09-11 07:56:01
【问题描述】:

我是 android 应用程序开发的初学者,我已经看到有两种方法可以处理按钮单击。 第一个使用setOnClickListener,第二个使用xml属性onClick

onClickxml 属性对初学者/我来说很容易。

我可以通过创建按钮对象来更改按钮文本,然后:btn.setText("Button Clicked");

但我的问题是我可以在不创建对象的情况下使用 setText、isEnabled 等按钮方法吗?

可以更改按钮文本无需创建按钮对象或使用 XML 属性onClick

【问题讨论】:

  • @Rohit M 你能用 setText / 任何其他方法解释更多,并给出代码示例 xml + java。
  • @Rohit M 如何使用片段实例在java中使用setText,请解释

标签: java android xml android-studio


【解决方案1】:
  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");
    }
}

【讨论】:

    【解决方案2】:

    这可以通过在 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>
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多