gpt4 book ai didi

java - 从另一个类调用 MainActivity 的方法?

转载 作者:行者123 更新时间:2023-11-30 08:46:17 27 4
gpt4 key购买 nike

我想从另一个类调用 MainActivity 的方法。方法是:

 public void setDate() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

Toast.makeText(this, "I am in on resume", Toast.LENGTH_LONG).show();


SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

date = sharedPreferences.getString("DATE_OF_JOURNEY", null);
if (date == null) {
sharedPreferences.edit().putString("DATE_OF_JOURNEY", day + "/" + month + "/" + year).apply();
date = day + "/" + month + "/" + year;
}

date_text.setText(date);
}

另一个类用作datePicker。 dateset 方法正在调用 MainActivity 的上述方法。

 public void onDateSet(DatePicker view, int year, int month, int day) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

Toast.makeText(getContext(),"The date is: " + year + month + day ,Toast.LENGTH_LONG).show();

sharedPreferences.edit().putString("DATE_OF_JOURNEY", day + "/" + month+"/"+ year).apply();
mainActivity.setDate();
// Do something with the date chosen by the user
}

基本上,我想获取 DatePickerFragment 类写入 MainActivitySharedPreferences 值。

编辑 1:MainActivity:

package com.example.shiza.cube26;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.prefs.PreferenceChangeEvent;

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;
TextView date_text;
String date;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url = "https://cube26-1337.0x10.info/stations";

date_text = (TextView) findViewById(R.id.date);



// new fetchData().execute(url);


}

public void setDate() {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

Toast.makeText(this, "I am in set date.", Toast.LENGTH_LONG).show();


SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

date = sharedPreferences.getString("DATE_OF_JOURNEY", null);
if (date == null) {
sharedPreferences.edit().putString("DATE_OF_JOURNEY", day + "/" + month + "/" + year).apply();
date = day + "/" + month + "/" + year;
}


date_text.setText(date);
}


@Override
protected void onResume() {
super.onResume();


}

public class fetchData extends AsyncTask<String, Void, String> {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar_timer);

@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}

@Override
protected String doInBackground(String... params) {
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

StringBuilder sb = new StringBuilder();

reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

String line;

while ((line = reader.readLine()) != null) {
sb.append(line + '\n');

}

return sb.toString();

} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);

try {
JSONObject json = new JSONObject(s);

JSONArray jsonArray = json.names();

for (int i = 0; i < jsonArray.length(); i++) {
Toast.makeText(getApplicationContext(), jsonArray.getInt(i), Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONArray jsonArray = new JSONArray(s);

for (int i = 0; i < jsonArray.length(); i++) {
Toast.makeText(getApplicationContext(), jsonArray.getString(i), Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {
e.printStackTrace();
}
}
}


public void showDatePickerDialog(View v) {

DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.setDateChangeListener(new DatePickerFragment.DateChangeListener(){
public void onDateChange(){
setDate();
}
});

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}

日期选择器 fragment :

package com.example.shiza.cube26;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.Toast;

import java.util.Calendar;

/**
* Created by Shiza on 04/10/2015.
*/
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

private DateChangeListener mListener;
public void setDateChangeListener(DateChangeListener listener){
mListener = listener;
}

public interface DateChangeListener{
public void onDateChange();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

Toast.makeText(getContext(),"The date is: " + year + month + day ,Toast.LENGTH_LONG).show();

sharedPreferences.edit().putString("DATE_OF_JOURNEY", day + "/" + month+"/"+ year).apply();
if(mListener!=null){
mListener.onDateChange();
}
// Do something with the date chosen by the user
}
}

最佳答案

你可以使用interface as callback机制在这里。我正在为具有以下示例的日期选择器对话框的类使用名称 OtherClass:

像这样在另一个类中定义一个接口(interface):

public interface DateChangeListener{
public void onDateChange();
}

并在同一个类中添加附加监听器的方法

private DateChangeListener mListener;
public void setDateChangeListener(DateChangeListener listener){
mListener = listener;
}

现在在同一类调用的 onDateSet

if(mListener!=null){
mListener.onDateChange();
}

在你的主要 Activity 中实现 DateChangeListener 接口(interface),然后像这样调用日期选择器对话框

   otherclassInstance.setDateChangeListener(new OtherClass.DateChangeListener(){
@override
public void onDateChange(){
setDate();
}
});

这样,只要在 OtherClass 中调用 onDateSet 方法,主 Activity 的 onDateSet 方法也会被调用。


编辑

像这样更改 showDatePickerDialog 方法:

public void showDatePickerDialog(View v) {

    DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.setDateChangeListener(new DatePickerFragment.DateChangeListener(){
public void onDateChange(){
setDate();
}
});

datePickerFragment .show(getSupportFragmentManager(), "datePicker");
}

关于java - 从另一个类调用 MainActivity 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32921808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com