添加另一个柜台系统 [英] Add another counter in the system
问题描述
我有这个项目的所有工作一个计数器,但我试图把另一个柜台,将做相同的,但在另算将工作在屏幕的底部。所以有会是两个计数器计数不同,但我无法得到它的工作。
XML code:
<的TextView
机器人:ID =@ + ID / TextViewCount
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:文字=@字符串/参考hello world/><按钮
机器人:ID =@ + ID / ButtonCount
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ + ID / TextViewCount
机器人:layout_alignRight =@ + ID / TextViewCount
机器人:layout_marginBottom =22dp
机器人:文字=计数/>
主要code:
包com.example.counter;进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;
公共类MainActivity延伸活动{
//私有成员字段来跟踪伯爵
私有静态诠释mCount = 0;私人TextView的countTextView;
私人按钮countButton;
公共静态最后弦乐preFS_NAME =com.example.myApp.mCount;
私人共享preferences设置= NULL;
私人共享preferences.Editor编辑= NULL; / **添加此方法** /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
countTextView =(的TextView)findViewById(R.id.TextViewCount);
countButton =(按钮)findViewById(R.id.ButtonCount); countButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
mCount ++;
countTextView.setText(伯爵:+ mCount);
编辑= settings.edit();
editor.putInt(mCount,mCount);
editor.commit();
}
});
设置= getShared preferences(preFS_NAME,MODE_PRIVATE);
} @覆盖
公共无效的onPause(){
super.onPause();
} @覆盖
公共无效onResume(){
super.onResume();
mCount = settings.getInt(mCount,0);
countTextView.setText(伯爵:+ mCount);
}
}
您需要线程来执行这个任务,每个柜台必须以一个单独的线程中运行。
做这样的事情:
线程t =新主题(新的Runnable(){ 公共无效的run(){
视频下载(1000);
反++;
}
});
t.start();
或使用的AsyncTask
更新:
再拍计数器和两个线程变量:
私有静态诠释mCount2 = 0;
私有静态主题MT1,MT2;
那么你需要一个方法,像这样:
私人无效startCounter(线程t,计数器){
T =新主题(新的Runnable(){ 公共无效的run(){
而(真){
视频下载(1000);
反++;
}
}
});
t.start();
}
您可以调用此方法在你的onclick法为每个线程的柜台,
那么你就必须停止线程中的onPause方法:
@覆盖
公共无效的onPause(){
super.onPause();
mT1.stop();
mT2.stop();
}
也许你需要的,如果条款来测试,如果线程运行实际上可以
I've got one counter in this project its all working but I'm trying to put another counter at the bottom of the screen that will do the same but will work on another count. So there are gonna be two counters counting differently, but I can't get it to work.
xml code:
<TextView
android:id="@+id/TextViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/ButtonCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/TextViewCount"
android:layout_alignRight="@+id/TextViewCount"
android:layout_marginBottom="22dp"
android:text="Count" />
Main Code:
package com.example.counter;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private static int mCount = 0;
private TextView countTextView;
private Button countButton;
public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;
/** ADD THIS METHOD **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countTextView = (TextView) findViewById(R.id.TextViewCount);
countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
editor = settings.edit();
editor.putInt("mCount", mCount);
editor.commit();
}
});
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
mCount = settings.getInt("mCount", 0);
countTextView.setText("Count: " + mCount);
}
}
You need threading to perform this task, each counter must run in an individual Thread.
Do something like that:
Thread t = new Thread(new Runnable(){
public void run() {
Thread.sleep(1000);
counter++;
}
});
t.start();
or use AsyncTask
UPDATE:
make another counter and two Thread variables:
private static int mCount2 = 0;
private static Thread mT1, mT2;
then you need a method, something like this:
private void startCounter(Thread t, counter) {
t = new Thread(new Runnable(){
public void run() {
while(true) {
Thread.sleep(1000);
counter++;
}
}
});
t.start();
}
you can call this method in your onclick-method with for each thread with the counter, then you just have to stop the Threads in the onPause method:
@Override
public void onPause() {
super.onPause();
mT1.stop();
mT2.stop();
}
maybe you need if clauses to test if the thread is acutally running.
这篇关于添加另一个柜台系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!