如果已填充,则禁用edittext,并使用setOnClickListener编辑文本

时间:2017-11-27 18:37:02

标签: java android android-edittext

我需要协助处理我正面临的edittext问题。当新用户进入时,edittext将正常响应。但是,一旦用户保存他/她的详细信息并退出应用程序并返回到同一页面;他们的详细信息将填入相应的edittext中。现在的问题是,如果填充了他们的信息,我不希望启用edittext。我想在用户更改edittext中的内容之前设置警告(警告对话框)。如果用户对警告命中为yes,我希望edittext可以再次编辑。

var app = angular.module("KryptoApp", ["ngRoute", "ui.router", "ngCookies", "ngAnimate"]);
app.config(function ($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('home', {
        url: '/',
        templateUrl: "/static/templates/index.html",
        controller: "IndexController"
    })
    .state('trade', {
        url: '/account/trade',
        templateUrl: "/static/templates/trade.html",
        controller: "TradeController"
    })
    .state('register', {
        url: '/register',
        templateUrl: "/static/templates/register.html",
        controller: "RegisterController"
    })
    .state('login', {
        url: '/login',
        templateUrl: "/static/templates/login.html",
        controller: "LoginController"
    })
    .state('trade.coin', {
        url: 'account/trade/coin',
        templateUrl: "/static/templates/trade_coin.html",
        controller: "TradeController"
    });

    $urlRouterProvider.otherwise('/'); 
});

app.config(function ($interpolateProvider) {
  $interpolateProvider.startSymbol('{[{');
  $interpolateProvider.endSymbol('}]}');
});

我想禁用" getInfo"中的edittext。方法并使用onClick方法启用它。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_personal__information);

    fName = findViewById(R.id.personal_info_fName_ET);
    lName = findViewById(R.id.personal_info_lName_ET);
    userAge = findViewById(R.id.personal_info_userAge_ET);
    userPhone = findViewById(R.id.personal_info_userPhone_ET);

    getInfo();

    fName.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            AlertDialog alertDialog = new AlertDialog.Builder(
                    Personal_Information.this).create();
            alertDialog.setTitle("Make Changes");
            alertDialog.setMessage("Do you want to make these changes?");
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "YES",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //fName.setFocusable(true);
                        }
                    });
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "NO",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
            return false;
        }
    });

    submit = findViewById(R.id.personal_info_submit_btn);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            seralizePersonalInfo("Users Personal Info");
        }
    });
}

我已尝试过可聚焦,禁用但onClickListener没有响应任何内容。我不想在XML中设置属性" focuseable = false"因为如果没有要显示的细节,我希望聚焦edittext。 提前致谢,如果您需要进一步澄清,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法来实现此目的

public void getInfo(){
    if (CurrentUserInfo.getFirstname() != null){
        fName.setText(CurrentUserInfo.getFirstname());
        lName.setText(CurrentUserInfo.getLastname());
        userAge.setText(CurrentUserInfo.getUserAge());
        userPhone.setText(CurrentUserInfo.getUserPhone());
        //do same for all edit text            
        fName.setKeyListener(null);
        fName.setLongClickable(false);
     }
}

并且您想要启用它只是给它们一个新的回调引用。

 BaseKeyListener mBaseKeyListener = new BaseKeyListener() {
    @Override
    public int getInputType() {
        return 0;
    }
};

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "YES",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //do same for all edit text            
                        fName.setKeyListener(mBaseKeyListener);
                        fName.setLongClickable(true);
                    }
                });