今天我在Android Studio预览版中发现了最近推出的双向数据绑定功能,并决定尝试一下.
我有一个非常简单的布局(下面的代码),用于撰写和发送消息.我想要实现的是当字段中没有输入文本时,按钮" 禁用 "(以及将来会有相应的图像).
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="msg" type="String"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/new_message_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingStart="10dp"
android:hint="@string/hint_compose_message"
android:inputType="textAutoCorrect|textMultiLine"
android:text="@={msg}"/>
<ImageButton
android:id="@+id/btn_send_message"
android:layout_width="50dp"
android:layout_height="match_parent"
android:src="@drawable/ic_send"
android:enabled="@{!new_message_input.text.isEmpty()}"
android:clickable="@{!new_message_input.text.isEmpty()}"/>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
第一个链接中的示例代码显示这样的内容应该足够了:
<layout ...>
<data>
<import type="android.view.View"/>
</data>
<RelativeLayout ...>
<CheckBox android:id="@+id/seeAds" .../>
<ImageView android:visibility="@{seeAds.checked ? View.VISIBLE : View.GONE}" .../>
</RelativeLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
但是,当尝试为enabled/ clickable属性实现类似的逻辑时ImageButton,我收到以下错误:
错误:
java.lang.RuntimeException:java.lang.RuntimeException:创建数据绑定错误.****/数据绑定错误****消息:标识符必须具有XML文件中的用户定义类型.new_message_input缺少它
问题肯定在于这两行,因为删除它们允许正确创建绑定类.
我的问题是:
我也尝试过不同的做法,但结果是一样的:
<?xml …Run Code Online (Sandbox Code Playgroud) data-binding android android-layout 2-way-object-databinding android-view
我已经阅读了关于Stack Overflow和其他地方的许多问题,这些问题描述了如何将文本框绑定到类,但是在编译时如果没有收到VS的错误,我似乎甚至无法使基础工作。
(1)我要完成的是显示一个类的属性的文本。
(2)当用户修改该文本时,我希望该属性自动更新。
不幸的是,我什至无法超越(1)。
班级:
class BookProperties : INotifyPropertyChanged
{
private string _bookTitle;
public string bookTitle { get { return _bookTitle; } set { SetField(ref _bookTitle, value, "bookTitle"); } }
#region handle property changes
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion …Run Code Online (Sandbox Code Playgroud) 在Angular2中,为什么我将一个属性放在一个简单的视图中,如下所示:
<span>{{allowServer}}</span>
Run Code Online (Sandbox Code Playgroud)
每当它的.ts文件的值发生变化时它就会改变,如果我把它改为:
<button class="btn btn-primary" disabled={{allowServer}} >server</button>
Run Code Online (Sandbox Code Playgroud)
该按钮没有禁用新值?
那么,我必须使用插值而不是绑定语法的规则是什么?
[disabled]=allowServer
Run Code Online (Sandbox Code Playgroud) 我经历了许多与数据绑定相关的帖子,但没有找到解决我问题的方法.我已经创建了一个示例应用程序来学习数据绑定.
预期行为:我有一个edittext和textview.textview应该更新我在edittext中写的任何内容.
问题:我创建了一个Observable并将其链接到edittext和textview.在edittext中编写时,我的textview没有更新.我在这里做错了.请检查以下代码 -
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setStudent(new Student("Rahul"));
activityMainBinding.executePendingBindings();
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="student"
type="com.rahulchaurasia.databindingtest.Student"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.rahulchaurasia.databindingtest.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@{student.name}"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{student.name}"/>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
Student.java
public class Student {
public ObservableField<String> name;
public Student(String n) {
name = new ObservableField<>(n);
}
} …Run Code Online (Sandbox Code Playgroud) InputCheckBox 我正在查看https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.inputcheckbox的文档,我发现它公开Value绑定到所需的布尔值(Gets or sets the value of the input. This should be used with two-way binding.)。尽管如此,到处都有人在使用@bind-Value,而且我无法Value上班。
这怎么样:
<InputCheckbox @bind-Value="model.IsSelected"></InputCheckbox>
Run Code Online (Sandbox Code Playgroud)
与此不同(以及为什么这个不起作用):
<InputCheckbox Value="@model.IsSelected"></InputCheckbox>
Run Code Online (Sandbox Code Playgroud)
我还注意到,@bind-Value更新/通知模型有关更改的信息,并更新依赖于 的任何属性IsSelected,而Value不会(可能除非明确指定?)。此外,在使用 时Value,我还需要ValueExpression为标签添加 a (否则它不会呈现)。这是什么ValueExpression??在什么情况下有人会实施不同的ValueExpression?
使用有Value什么好处吗?需要什么才能让它发挥作用?我在这里错过了什么吗?
data-binding checkbox binding 2-way-object-databinding blazor
我正在尝试为WebView和Progressbar实现双向数据绑定.
最初ProgressBar将继续出现,一旦webview完成加载,进度条应该是GONE.
但我无法创建那种绑定
我已经为webview创建了Binding适配器来加载url,并且还设置了WebViewClient以检查页面加载是否已完成但无法更新进度条的可见性
//////////////发布
@BindingAdapter({"app:webUrl"})
public void configureWebView(WebView iWebView, String iUrl) {
iWebView.getSettings().setJavaScriptEnabled(true);
iWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// code to set visibility of progress bar
}
});
iWebView.loadUrl(iUrl);
}
Run Code Online (Sandbox Code Playgroud)
////////////////////布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="newsUrl"
type="com.example.bindingdemo.data.model.Post" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/news_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:webUrl="@{newsUrl.url}" />
<ProgressBar
android:id="@+id/news_prog_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{**<WhatConditionToWrite>** ? View.GONE: View.Visible}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout> …Run Code Online (Sandbox Code Playgroud) android ×3
data-binding ×3
android-view ×1
angular ×1
binding ×1
blazor ×1
c# ×1
checkbox ×1
webview ×1
winforms ×1