英文:
Update TextView once alertdialog is dismissed
问题
我有一个名为DiscoverActivity的活动,其中有以下距离TextView
:
(图片链接)
一旦用户点击距离,将会打开以下的AlertDialog
:
(图片链接)
现在,我希望用户点击“完成”按钮后,它将关闭AlertDialog
并更新DiscoverActivity上的TextView
。
我该如何更新它?
当前,当我点击“完成”并关闭后,它会显示相同的值,直到我在活动之间切换并返回。
我的“完成”按钮的代码:
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();
} );
以下是我在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 );
} );
英文:
I have an activity (called DiscoverActivity) that has the following distance TextViews
:
Once the users click on the distance, the following AlertDialog
opens:
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
.
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:
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 );
} );
答案1
得分: 1
任何方法,包括共享首选项(shared preferences),只能在刷新活动之前起作用一次,但是当您关闭对话框时,活动不会被刷新。
因此,您有两个选择:
- 寻找一种方法,在警示对话框中的“完成”按钮被点击以关闭对话框后,刷新活动。
- 或者您可以保持一切不变,但是当用户点击“完成”按钮时,更改活动的文本。
为了执行第二点,因为它更简单,您可以这样做:
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();
});
英文:
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
- 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();
} );
答案2
得分: 0
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类中
tv_NearRadius.setOnClickListener( v -> {
PopUps popUps = new PopUps();
popUps.popDistanceDialog( DiscoverActivity.this, DiscoverActivity.this);
} );
class PopUps{
public void popDistanceDialog( Context context, SomeInfy infy ){
// 无论是btnDone的操作...
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");
} );
}
英文:
Define an interface,
interface SomeInfy(){
void updateText(String text);
}
Implement an interface to your DiscoveryActivity and override updateText()
@Override
public void updateText(String someText){
tv_NearRadius.setText(someText);
}
Now,
Just pass an interface from your DiscoveryActivity to 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");
} );
}
答案3
得分: 0
或者更加残酷,但也可以很方便。您的对话框具有getActivity()方法。
就在关闭对话框之前,在保存了您的首选项之后,使用来自对话框的getActivity()方法。将其转换为您的活动,然后更新您的编辑文本。
正如我所说,这是原始的编程方法,但可能适用于您的项目的规模和结构。
玩得开心。
英文:
Or more brutal but can be convenient too. Your dialog has getActivity() method.
Just before dismiss and after saved your preference, use getActivity() method from your dialog. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论