怎样才能让2类之间的关系在一个程序?(机器人) [英] How can make a relation between 2 class in one program?(android)

查看:119
本文介绍了怎样才能让2类之间的关系在一个程序?(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写与Android程序可以找到其他SSID的,并告诉他们。我有2个XML页面desinged它。我创建了第2页的ImageButton,并希望进行的ImageButton和搜索方法之间的关系。这意味着我要点击的ImageButton和seaching方法开始它的工作和搜索SSID的,并告诉他们...
我的问题是,我下载我的搜索方法,因为这样的话,我可以不承认我必须在我写在第二页的ImageButton的我setonclick方法调用哪个方法?我尝试单独创建另一个类的搜索方法,并从第二类第2页中调用它。
但我不知道我怎样才能使这些CALSS 2(我指的是第二和第三类)之间的关系。或者我必须写在一个类中我的ImageButton的点击搜索和方法?
感谢您的suggestion.this是code,我是复制:

 进口java.util.Date;
进口的java.util.List;
进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.net.wifi.ScanResult;
进口android.net.wifi.WifiManager;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;公共类wifiScan延伸活动{    私有类WifiReceiver扩展广播接收器{
        @覆盖
      公共无效的onReceive(上下文C,意图意图){
        清单< ScanResult>结果= wifi.getScanResults();
        日期tempDate =新的日期();
        字符串信息= testNumber ++(tempDate.getTime() - testDate。
          的getTime())++ results.size();
        Log.i(wifiScan,信息);
        wifiText.setText(信息);
        testNumber ++;
        testDate =新的日期();
        wifi.startScan();        }
    }
    私人TextView的wifiText;
    私人WifiManager无线网络;
    私人WifiReceiver接收机;
    私人日期testDate;
    私有静态诠释testNumber = 0;
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        testNumber = 0;        wifiText =(的TextView)findViewById(R.id.wifiText);
        接收器=新WifiReceiver();
        registerReceiver(接收器,新IntentFilte
           (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        如果(!wifi.isWifiEnabled()){            wifi.setWifiEnabled(真);          }
        开始扫描();
    }
    @覆盖
    公共无效的onStop(){
        super.onStop();
        完();
    }
    公共无效startScan(){
        testDate =新的日期();
        wifi.startScan();
    }
}


解决方案

  you_image_button.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
    意图I =新意图(这一点,wifiScan.class);
    。getApplicationContext()startActivity(ⅰ);
            }
        });

I must write a program with android which can find other ssid's and show them. I desinged it with 2 xml pages. I create an imagebutton in page2 and want to make a relation between imagebutton and searching method. It means i want to click on imagebutton and seaching method begin it's work and search ssid's and show them... My problem is, I download my search method and because of that i can not recognize which method i must call on my setonclick method that i write for an imagebutton in second page? I try to create another class seperately for search method and call it from the second class of page2. but i dont know how can i make a relation between these 2 calss(i mean the second and third class). Or i must write the method of searching and on click of my imagebutton in one class? Thanks for your suggestion.this is the code that i was copy:

import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class wifiScan extends Activity {

    private class WifiReceiver extends BroadcastReceiver{
        @Override
      public void onReceive(Context c, Intent intent) {
        List<ScanResult> results = wifi.getScanResults();
        Date tempDate=new Date();
        String info=testNumber+" "+(tempDate.getTime()-testDate.
          getTime()) +" "+results.size();
        Log.i("wifiScan", info);    
        wifiText.setText(info);
        testNumber++;
        testDate=new Date();
        wifi.startScan();

        }
    }
    private TextView wifiText;
    private WifiManager wifi;
    private WifiReceiver receiver;
    private Date testDate;
    private static int testNumber=0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        testNumber=0;

        wifiText = (TextView) findViewById(R.id.wifiText);
        receiver=new WifiReceiver();
        registerReceiver(receiver, new IntentFilte
           (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
        if(!wifi.isWifiEnabled()){

            wifi.setWifiEnabled(true);

          }
        startScan();


    }
    @Override
    public void onStop(){
        super.onStop();
        finish();
    }
    public void startScan(){
        testDate=new Date();
        wifi.startScan();
    }
}

解决方案

you_image_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    Intent i = new Intent(this, wifiScan.class);
    getApplicationContext().startActivity(i);
            }
        });

这篇关于怎样才能让2类之间的关系在一个程序?(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆