1

我需要一个应该有两个功能的按钮。

如果我点击它一次,它将转到下一页。如果我按住按钮,它将允许我编辑按钮上的文本。

按钮可以在点击后转到下一页,但是如何实现第二个功能,如果我按住按钮,我可以更改文本?

有人知道吗?

爪哇代码

public class MainActivity extends Activity {
    Button button1;
    Button button2;
    Button button3;
    Handler h;
    private Socket socket;
    private boolean mInSettingsMode;
    private static final int SERVERPORT = 5000;
    private static final String SERVER_IP = "192.168.43.83";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new ClientThread()).start();
        // living button click start
        button1 = (Button) findViewById(R.id.btnliving);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                h = new Handler();
                h.postDelayed(irun, 0);
            }
        });

        button2 = (Button) findViewById(R.id.btnbed);
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                h = new Handler();
                h.postDelayed(irun2, 0);
            }
        });

        button3 = (Button) findViewById(R.id.btndin);
        button3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                h = new Handler();
                h.postDelayed(irun3, 0);
            }
        });
    }

    Runnable irun = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, living.class);
            startActivity(i);
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    };

    Runnable irun2 = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, bed.class);
            startActivity(i);
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    };

    Runnable irun3 = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, din.class);
            startActivity(i);
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    };

    /*
     * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
     * menu; this adds items to the action bar if it is present.
     * getMenuInflater().inflate(R.menu.main, menu); return true; }
     */
    class ClientThread implements Runnable {
        @Override
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                // new Thread(new ClientThread()).start();
                socket = new Socket(serverAddr, SERVERPORT);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:gravity="center"
 android:orientation="vertical"
 android:background="@drawable/back1"
 tools:context=".MainActivity" >
 <Button 
 android:id="@+id/btnliving"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Light Control"
 />
 <Button 
 android:id="@+id/btnbed"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Fan Control"
 />
 <Button 
 android:id="@+id/btndin"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Thermostat Control"
 />

 </LinearLayout>
4

2 回答 2

4

您可以将第二个函数设置为 setOnLongClickListener 为:

button.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            //change text here
            return true;
        }
    });

第一个函数与 setOnclickListener 相同:

button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

检查此文档

于 2013-10-18T06:01:04.720 回答
1
button.setOnTouchListener(new View.OnTouchListener() {

    private static final long MAX_CLICK_TIME = 400;
    long time = 0;

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            time = System.currentTimeMillis();
        } else if (MotionEvent.ACTION_UP == event.getAction()) {
            long pressingTime = System.currentTimeMillis() - time;
            if (pressingTime > MAX_CLICK_TIME) {
                // do  change text logic
            } else {
                // show new page logic
            }
        }
        return true;
    }
});

如果您想更改按钮的文本而不必等到用户将手指从按钮上移开,您可以将 TimerTask.Code 与 TimerTask 一起使用:

        button.setOnTouchListener(new View.OnTouchListener() {

        private static final long MAX_CLICK_TIME = 400;
        long time = 0;
        Timer timer = new Timer();

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (MotionEvent.ACTION_DOWN == event.getAction()) {
                time = System.currentTimeMillis();
                TimerTask timerTask = new TimerTask() {
                    @Override
                    public void run() {
                        // do  change text logic
                    }
                };
                timer.schedule(timerTask, MAX_CLICK_TIME);
            } else if (MotionEvent.ACTION_UP == event.getAction()) {
                long pressingTime = System.currentTimeMillis() - time;
                if (pressingTime <= MAX_CLICK_TIME) {
                    timer.cancel();
                    // show new page logic
                }
            }
            return true;
        }
    });

我没有测试过,但应该可以。无论如何,这个想法应该是清楚的。

于 2013-10-18T05:51:49.620 回答