微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何使用数据绑定请求或删除对edittext的关注

我正在学习mvvm结构并使用mvvm结构和数据绑定制作应用程序.

现在,我想要做的是,我想从sharedpreference获取用户,如果我成功获得用户,那么我将usr的名称设置为edittext1.在这种情况下,我想请求专注于edittext2.

How to achieve that using databinding? (in such a way that i don’t
have to use activity. The job should be done using view model and xml
only.)

我尝试过使用以下方式.

StartGameviewmodel

public class StartGameviewmodel extends viewmodel {

    public static String TAG="StartGameviewmodel";

    private Preference<User> preference;
    public ObservableField<String> player1Name=new ObservableField<>("");
    public ObservableField<String> player2Name=new ObservableField<>("");

    public ObservableBoolean shouldRequestFocus=new ObservableBoolean(false);


    StartGameviewmodel(Preference preference){
        this.preference=preference;
    }

    public void getPreference() {
        preference.get(Constants.CURRENT_USER,User.class)
                .subscribe(new Observer<User>() {
                    @Override
                    public void onSubscribe(disposable d) {

                    }

                    @Override
                    public void onNext(User user) {
                        player1Name.set(user.name);
                        shouldRequestFocus.set(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                       Log.i(TAG,"user is logged out");
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG,"Completed Preference");
                    }
                });
    }
}

activity_start_game.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".view.StartGameActivity">

    <data>
        <variable
            name="startGameviewmodel"
            type="com.sevenbits.android.mvvmsample.viewmodel.StartGameviewmodel" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Players Name"
            android:textSize="24sp"
            android:textColor="@color/white"
            app:layout_constraintVertical_bias="0.75"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/input_layout"
            app:layout_constraintTop_toTopOf="parent"
            />

        <LinearLayout
            android:id="@+id/input_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:background="@color/white"
            android:elevation="20dp"
            android:orientation="vertical"
            android:paddingBottom="40dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.45"
            tools:layout_editor_absoluteX="8dp">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/til_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/et_player1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Player1"
                    android:text="@{startGameviewmodel.player1Name}"
                    app:addTextChangedListener="@{startGameviewmodel.Player1Watcher}" />

            </android.support.design.widget.TextInputLayout>


            <android.support.design.widget.TextInputLayout
                android:id="@+id/til_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/et_player2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Player2"
                    android:text="@{startGameviewmodel.player2Name}"
                    app:addTextChangedListener="@{startGameviewmodel.Player2Watcher}" />

            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <Button
            android:id="@+id/start_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_button"
            android:elevation="20dp"
            android:gravity="center"
            android:paddingBottom="20dp"
            android:paddingEnd="40dp"
            android:paddingStart="40dp"
            android:paddingTop="20dp"
            android:text="Start"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/input_layout" />

    </android.support.constraint.ConstraintLayout>
</layout>

StartGameActivity:

public class StartGameActivity extends AppCompatActivity {

    ActivityStartGameBinding activityStartGameBinding;
    StartGameviewmodel startGameviewmodel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDataBinding();
        setStartGameListener();
    }

    private void initDataBinding() {
        activityStartGameBinding = DataBindingUtil.setContentView(this,R.layout.activity_start_game);

        StartGameviewmodelFactory startGameviewmodelFactory= Injection.provideStartGameviewmodelFactory(this);
        startGameviewmodel = viewmodelProviders.of(this,startGameviewmodelFactory).get(StartGameviewmodel.class);
        activityStartGameBinding.setStartGameviewmodel(startGameviewmodel);

        startGameviewmodel.getPreference();
        if(startGameviewmodel.shouldRequestFocus.get())
            findViewById(R.id.et_player2).requestFocus();
    }

    private void setStartGameListener() {
        findViewById(R.id.start_button).setonClickListener(v -> {
            Intent intent=new Intent(StartGameActivity.this,GameActivity.class);
            intent.putExtra(Constants.PLAYER1_NAME,startGameviewmodel.player1Name.get());
            intent.putExtra(Constants.PLAYER2_NAME,startGameviewmodel.player2Name.get());
            startActivity(intent);
        });
    }
}

解决方法

通过绑定适配器非常简单

@JvmStatic
@BindingAdapter("requestFocus")
fun requestFocus(view: TextView,requestFocus: Boolean) {
    if (requestFocus) {
        view.isFocusableInTouchMode = true
        view.requestFocus()
    }
}



             <EditText
                android:id="@+id/et_company"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:imeOptions="actionDone"
                android:inputType="textCapSentences"
                android:maxLength="32"
                android:scrollbars="vertical"

                app:requestFocus="@{company.requestFocus}" />

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐