简体   繁体   English

关闭 alertdialog 后更新 TextView

[英]Update TextView once alertdialog is dismissed

I have an activity (called DiscoverActivity) that has the following distance TextViews :我有一个具有以下距离TextViews的活动(称为 DiscoverActivity):

在此处输入图像描述

Once the users click on the distance, the following AlertDialog opens:用户单击距离后,将打开以下AlertDialog

在此处输入图像描述

Now, I had like that once the users click on the Done button, that it will dismiss the AlertDialog and update the TextView on DiscoverActivity .现在,一旦用户单击“ Done按钮,我就喜欢这样,它将关闭AlertDialog并在DiscoverActivity上更新TextView

How can I update it?我该如何更新它?

Right now when I click Done and dismiss it shows the same value until I move between activities and return.现在,当我单击完成并关闭它时,它会显示相同的值,直到我在活动之间移动并返回。

My code to the Done button:我的完成按钮的代码:

Btn_Done.setOnClickListener( view -> {
    SharedPreferences.Editor editor = context.getSharedPreferences( AppConstants.PREFS_NAME, MODE_PRIVATE ).edit();
    editor.putInt( AppConstants.PREF_RADIUS, seekbar.getProgress() );
    editor.apply();
    dialog.dismiss();
} );

And this is what I call in my onCreate:这就是我在 onCreate 中所说的:

SharedPreferences dist_settings = getSharedPreferences( AppConstants.PREFS_NAME, MODE_PRIVATE );
nearMeRadius = dist_settings.getInt( AppConstants.PREF_RADIUS, 300 );
TextView tv_NearRadius = findViewById( R.id.tv_NearRadius );
tv_NearRadius.setText( getString( R.string.ActivityDiscover_NearMeRadius, nearMeRadius ) );
tv_NearRadius.setOnClickListener( v -> {
    PopUps popUps = new PopUps();
    popUps.popDistanceDialog( DiscoverActivity.this );
} );

Any method including shared prefrences will only work once until activity is refreshed but when you dismiss a dialog the activity isn't refreshed.包括共享首选项在内的任何方法都只能工作一次,直到刷新活动,但是当您关闭对话框时,活动不会刷新。

So, you have 2 options所以,你有2个选择

  • Search for a way to refresh the activity once the done button in the alert dialog is clicked to dismiss the dilaog.单击警报对话框中的完成按钮以关闭对话框后,搜索刷新活动的方法。
  • Or you can just keep everything the same but when the user clicks done button it changes the text of the activity.或者您可以保持一切不变,但是当用户单击完成按钮时,它会更改活动的文本。

In order to do the second point sense it is much easier you can just do:为了做到第二点,你可以更容易地做到:

Btn_Done.setOnClickListener( view -> {
    SharedPreferences.Editor editor = context.getSharedPreferences( AppConstants.PREFS_NAME, MODE_PRIVATE ).edit();
    editor.putInt( AppConstants.PREF_RADIUS, seekbar.getProgress() );
    editor.apply();
    TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text); // this points to the textview in the activity you want to change
        txtView.setText("Hello");
    dialog.dismiss();
} );

Define an interface,定义一个接口,

interface SomeInfy(){
void updateText(String text);
}

Implement an interface to your DiscoveryActivity and override updateText()实现 DiscoveryActivity 的接口并覆盖 updateText()

@Override
public void updateText(String someText){
tv_NearRadius.setText(someText);
}

Now, Just pass an interface from your DiscoveryActivity to PopUps class现在,只需将 DiscoveryActivity 中的接口传递给 PopUps class

tv_NearRadius.setOnClickListener( v -> {
    PopUps popUps = new PopUps();
    popUps.popDistanceDialog( DiscoverActivity.this, DiscoverActivity.this);
} );


class PopUps{

public void popDistanceDialog( Context context, SomeInfy infy ){


// whatever btnDone operation...
 Btn_Done.setOnClickListener( view -> {
    SharedPreferences.Editor editor = context.getSharedPreferences( AppConstants.PREFS_NAME, MODE_PRIVATE ).edit();
    editor.putInt( AppConstants.PREF_RADIUS, seekbar.getProgress() );
    editor.apply();
    dialog.dismiss();
    
    //
    infy.updateText("whatever");
    
    
} );



}

Or more brutal but can be convenient too.或者更残酷但也可以很方便。 Your dialog has getActivity() method.您的对话框具有 getActivity() 方法。

Just before dismiss and after saved your preference, use getActivity() method from your dialog.在关闭之前和保存您的首选项之后,使用对话框中的 getActivity() 方法。 Cast it to your activity and then update your edit text.将其投射到您的活动中,然后更新您的编辑文本。

Like I said, that raw programming but could suitable to the size of and structure of your project.就像我说的那样,原始编程但可能适合您项目的大小和结构。

Have fun.玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM