sách gpt4 ai đã đi

Android更新textview计时器并刷新 map 内容?

In lại 作者:行者123 更新时间:2023-11-30 00:43:30 31 4
mua khóa gpt4 Nike

我有以下布局文件。基本上我有谷歌地图,在左上角我有一个 TextView,我需要在其中每 15 秒保持一次计数器以刷新 map 。布局很好。


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="phù hợp với cha mẹ"
android:layout_height="match_parent" >


xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="phù hợp với cha mẹ"
android:layout_height="phù hợp với cha mẹ"
android:id="@+id/map"
tools:context="com.example.ns.appversion1.MapActivityFragment1"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

<>
android:layout_width="phù hợp với cha mẹ"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >


android:layout_width="phù hợp với cha mẹ"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="2" >


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:padding="3dp"
android:id="@+id/textRefresh"
android:background="@drawable/border"
android:text="Refreshing 1"
android:textColor="#03A9FA"
android:textSize="12sp" />





下面是我的代码。在 View 函数中,我尝试实现一个 Người xử lý 函数来保持计数器。因此,我想每隔 15 秒调用一次 JSON 函数来检索最新的经纬度值并相应地绘制在 map 上。我现在卡在第一步了,它给了我错误提示无法从内部类访问变量 timeleft,m_handlerTaskm_handler?我想显示倒计时,以便用户知道在接下来的 15 秒内计数器将刷新 map ?

public class MapActivityFragment1 extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
TextView tv;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public MapActivityFragment1() {
// Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static MapActivityFragment1 newInstance(String param1, String param2) {
MapActivityFragment1 fragment = new MapActivityFragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Ghi đè
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false);

SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tv = (TextView) getActivity().findViewById(R.id.textRefresh);
Handler m_handler;
Runnable m_handlerTask ;
final int timeleft=15000;
final m_handler = new Handler();
final m_handlerTask = new Runnable()
{
@Ghi đè
công khai void run() {
if(timeleft>=0)
{
// làm gì đó
Log.i("timeleft",""+timeleft);
timeleft--;
tv.setText("Refreshing in "+timeleft);

}
khác
{
m_handler.removeCallbacks(m_handlerTask); // cancel run
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();

quay lại xem;
}
@Ghi đè
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}


public void onAttach(Context context) {
super.onAttach(context);
}

@Ghi đè
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

我试过 Charu 的解决方案如下。

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map_activity_fragment1, null, false);

SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
tv = (TextView) getActivity().findViewById(R.id.textRefresh);
MyCount counter = new MyCount(15000, 1000);
counter.start();

quay lại xem;
}

1 Câu trả lời

这将演示从 15 到 0 的倒计时(每秒 -1),此任务将在每 15 秒后再次重复!

    public class YourActivity extends Activity {


TextView textView;
android.os.Handler customHandler;

@Ghi đè
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView = (TextView) findViewById(R.id.text);


customHandler = new android.os.Handler();
customHandler.postDelayed(updateTimerThread, 0);
}


// count down timer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Ghi đè
public void onFinish() {
textView.setText("done!");
}

@Ghi đè
public void onTick(long millisUntilFinished) {
textView.setText("Left: " + millisUntilFinished / 1000);
}
}

private Runnable updateTimerThread = new Runnable()
{
public void run()
{

// 10000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(15000, 1000);
counter.start();
customHandler.postDelayed(this, 15000); // repeat body after 15 seconds
}
};
}

关于Android更新textview计时器并刷新 map 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42190255/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com