当我在一个活动中使用多个布局时,imeOptoins是否不能正常工作?

时间:2019-02-23 18:56:38

标签: java android

我正在制作instagram克隆应用。在介绍中,几个屏幕的设计非常简单。因此,我认为进行4个活动可能会很混乱。因此,我尝试仅进行一项活动并利用setContentView()并更改活动的布局。

但是,似乎imeOptions不起作用。我添加了android:imeOptions="actionDone""actionNext"。它不起作用,因此,我以编程方式在setImeOptions(EditorInfo.xxx)中添加了该选项。

为什么它们不起作用?

但是,当我单击enter键时,编辑文本将变为空。

我已经尝试过OnKeyListener,但效果很好。

这是FirstActivity.java的完整代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.simplesns.simplesns.GlobalUser;
import org.simplesns.simplesns.R;
import org.simplesns.simplesns.activity.main.MainActivity;
import org.simplesns.simplesns.activity.sign.item.SignUpResult;
import org.simplesns.simplesns.activity.sign.item.ValidResult;
import org.simplesns.simplesns.item.MemberItem;
import org.simplesns.simplesns.lib.remote.RemoteService;
import org.simplesns.simplesns.lib.remote.ServiceGenerator;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FirstActivity extends AppCompatActivity {
    private static String TAG = FirstActivity.class.getSimpleName();
    int backCount = 0;

    String email;
    String username;
    String password;

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

    @Override
    public void onBackPressed() {
        backCount++;

        switch (backCount) {
            case 0:
                initFirstEmailValid();
                break;
            case 1:
                initFirst();
                break;
            case 2:
                Toast.makeText(this, "Press back to exit.", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                super.onBackPressed();
                finish();
                break;
        }
    }

    public void initFirst() {
        setContentView(R.layout.activity_first);
        Button createBTN = findViewById(R.id.create_button_firstactivity);
        Button loginBTN = findViewById(R.id.login_button_firstactivity);

        createBTN.setOnClickListener((v) -> {
            initFirstEmailValid();
        });

        loginBTN.setOnClickListener((v) -> {
            initLogin();
        });
    }

    public void initFirstEmailValid() {
        backCount = 0;
        setContentView(R.layout.activity_first_email_valid);
        EditText emailET = findViewById(R.id.email_edittext_first_email_valid);
        Button nextBTN = findViewById(R.id.next_button_first_email_valid);

        if (email != null) {
            emailET.setText(email);
        }

        emailET.setImeOptions(EditorInfo.IME_ACTION_DONE);
        // Why it's not working?
        emailET.setOnEditorActionListener((textView, i, keyEvent) -> {
            if(i == EditorInfo.IME_ACTION_DONE){
                nextBTN.performClick();
            }
            return false;
        });

        nextBTN.setOnClickListener((v) -> {
            email = emailET.getText().toString();

            try {
                validateEmail(email);
//                initFirstCreate(email);
            } catch (NullPointerException e) {
                Toast.makeText(FirstActivity.this, "Input your email.", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void validateEmail(String to) {
        Log.d(TAG, "validateEmail()= to(email): " + email);

        // backCount 세지 않음.
        RemoteService remoteService = ServiceGenerator.createService(RemoteService.class);

        Call<ValidResult> call = null;
        try {
            call = remoteService.validateEmail(to);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        try {
            call.enqueue(new Callback<ValidResult>() {
                @Override
                public void onResponse(Call<ValidResult> call, Response<ValidResult> response) {
                    Log.d(TAG, "onResponse()");
                    try {
                        ValidResult validResult = response.body();
                        Log.d(TAG, validResult.toString());

                        switch (validResult.getCode()) {
                            case 100:
                                initFirstCreate(email);
                                Toast.makeText(FirstActivity.this, validResult.getMessage(), Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                Toast.makeText(FirstActivity.this, validResult.getMessage(), Toast.LENGTH_SHORT).show();
                                break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ValidResult> call, Throwable throwable) {
                    throwable.printStackTrace();
                    Toast.makeText(FirstActivity.this, throwable.toString(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initFirstCreate(String email) {
        Log.d(TAG, "initFirstCreate()= email: " + email);
        backCount = -1;
        setContentView(R.layout.activity_first_create);
        TextView infoTV = findViewById(R.id.info_textview_first_create_activity);
        EditText usernameET = findViewById(R.id.fullname_edittext_first_create_activity);
        EditText passwordET = findViewById(R.id.password_edittext_first_create_activity);
        Button continueBTN = findViewById(R.id.continue_button_first_create_activity);

        infoTV.setText(Html.fromHtml("Your contacts will be periodically synced and stored on instagram servers to help you and others find friends, and to help us provide a better service. To remove contacts, go to Settings and disconnect. <a href=''>Learn More</a>"));

        if (username != null) {
            usernameET.setText(username);
        }

        if (password != null) {
            passwordET.setText(password);
        }

        passwordET.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                continueBTN.performClick();
            }
            return false;
        });

        continueBTN.setOnClickListener((v) -> {
            try {
                username = usernameET.getText().toString();
                try {
                    password = passwordET.getText().toString();
                } catch (NullPointerException e) {
                    Toast.makeText(FirstActivity.this, "Input your password.", Toast.LENGTH_SHORT).show();
                }
            } catch (NullPointerException e) {
                Toast.makeText(FirstActivity.this, "Input your name.", Toast.LENGTH_SHORT).show();
            }

            tempPass();

            try {
                MemberItem memberItem = new MemberItem(email, username, password);

                RemoteService remoteService = ServiceGenerator.createService(RemoteService.class);

                Call<SignUpResult> call = remoteService.insertMember(memberItem);

                call.enqueue(new Callback<SignUpResult>() {
                    @Override
                    public void onResponse(Call<SignUpResult> call, Response<SignUpResult> response) {
                        SignUpResult signUpResult = response.body();
                        try {
                            Log.d(TAG, signUpResult.toString());
                            switch (signUpResult.code) {
                                case 100:
                                    try {
                                        GlobalUser.getInstance().login(FirstActivity.this, email, passwordET.getText().toString());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        Toast.makeText(FirstActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                    break;
                                default:
                                    Toast.makeText(FirstActivity.this, signUpResult.code + ": " + signUpResult.message, Toast.LENGTH_SHORT).show();
                                    break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(FirstActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<SignUpResult> call, Throwable throwable) {
                        throwable.printStackTrace();
                        Toast.makeText(FirstActivity.this, throwable.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(FirstActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void initLogin() {
        backCount = 0;
        setContentView(R.layout.activity_login);

        EditText usernameET = findViewById(R.id.username_edittext_loginactivity);
        EditText passwordET = findViewById(R.id.password_edittext_loginactivity);
        Button loginBTN = findViewById(R.id.login_button_loginactivity);

        loginBTN.setOnClickListener((v) -> {
            if (usernameET.getText().toString() != null && passwordET.getText().toString() != null) {
                GlobalUser.getInstance().setMyId(usernameET.getText().toString());
                tempPass();
            } else {
                Toast.makeText(this, "Please input your name and password.", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void tempPass() {
        Intent intent = new Intent(FirstActivity.this, MainActivity.class);
        GlobalUser.getInstance().setMyId(username);
        startActivity(intent);
        finish();
    }
}

这是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">

    <data>

    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        tools:context="org.simplesns.simplesns.activity.LoginActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/logo_imageview_firsttwoactivity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:text="NAME AND PASSWORD"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/fullname_edittext_first_create_activity"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/outline"
                android:hint="@string/fullname"
                android:padding="15dp"
                android:imeOptions="actionNext"
                android:textAllCaps="false"
                android:textColor="@color/link_blue"
                android:textColorHint="@color/grey2" />

            <EditText
                android:id="@+id/password_edittext_first_create_activity"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/outline"
                android:hint="@string/cpassword"
                android:padding="15dp"
                android:imeOptions="actionNext"
                android:textAllCaps="false"
                android:textColor="@color/link_blue"
                android:textColorHint="@color/grey2" />

            <EditText
                android:id="@+id/code_edittext_first_create_activity"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/outline"
                android:hint="@string/code"
                android:padding="15dp"
                android:imeOptions="actionDone"
                android:textAllCaps="false"
                android:textColor="@color/link_blue"
                android:textColorHint="@color/grey2" />

            <Button
                android:id="@+id/continue_button_first_create_activity"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="@dimen/default_side_margin"
                android:layout_marginRight="@dimen/default_side_margin"
                android:layout_marginBottom="30dp"
                android:background="@color/link_blue"
                android:text="Continue and Sync Contacts"
                android:textAllCaps="false"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Continue Without Syncing Contacts"
                android:textColor="@color/link_blue" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="2dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/info_textview_first_create_activity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:autoLink="web"
                android:gravity="center"
                android:paddingLeft="@dimen/default_side_margin"
                android:paddingRight="@dimen/default_side_margin"
                android:paddingBottom="20dp"
                android:text="information"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/grey2" />
        </LinearLayout>
    </RelativeLayout>
</layout>

0 个答案:

没有答案