为什么我的按钮无法获得焦点,除非我先点击另一个按钮?

时间:2013-05-15 01:00:31

标签: java android xml button focus

我的活动中有5个按钮,其中一个是完成()活动的关闭按钮。除非我先单击其中一个按钮然后单击它,否则单击它不会执行任何操作。我假设问题出在我的xml中。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="3.05"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/siteName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/dialogsitename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="8.05"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/siteAddress"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/dialogaddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/userName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/dialogusername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/dialogpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26dp"
            android:layout_weight="58.17"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/port"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/dialogport"
            android:layout_width="54dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <CheckBox
            android:id="@+id/dialogpassive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="Passive" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/testBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Test Connection" />

        <Button
            android:id="@+id/saveSite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Save"
            android:visibility="visible" />

        <Button
            android:id="@+id/closeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Close" />

        <Button
            android:id="@+id/connectBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Connect" />

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="440dp"
    android:layout_height="401dp"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/siteList"
        android:layout_width="match_parent"
        android:layout_height="320dp" >
    </ListView>

    <Button
        android:id="@+id/clrBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Sites" />

</LinearLayout>

代码:

public class SiteManager extends Activity {
    DBAdapter myDb;
    public FTPClient mFTPClient = null;

    public EditText sitename;
    public EditText address;
    public EditText username;
    public EditText password;
    public EditText port;
    public CheckBox passive;
    public ListView site_list;
    public TextView tview;
    public Button clr;
    public Button test;
    public Button savesite;
    public Button close;
    public Button connect;

    String _name;
    String _address;
    String _username;
    String _password;
    int _port;
    int _passive = 0;

    List<FTPSite> model = new ArrayList<FTPSite>();
    ArrayAdapter<FTPSite> adapter;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.site_manager);

        site_list = (ListView) findViewById(R.id.siteList);

        adapter = new SiteAdapter(this, R.id.ftpsitename, R.layout.siterow,
                model);
        site_list.setAdapter(adapter);
        site_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {
                final FTPSite item = (FTPSite) parent
                        .getItemAtPosition(position);
                String tmpname = item.getName();
                String tmpaddress = item.getAddress();
                String tmpuser = item.getUsername();
                String tmppass = item.getPassword();
                int tmpport = item.getPort();
                String tmp_port = Integer.toString(tmpport);
                int tmppassive = item.isPassive();

                sitename.setText(tmpname);
                address.setText(tmpaddress);
                username.setText(tmpuser);
                password.setText(tmppass);
                port.setText(tmp_port);
                if (tmppassive == 1) {
                    passive.setChecked(true);
                } else {
                    passive.setChecked(false);
                }
            }

        });
        sitename = (EditText) findViewById(R.id.dialogsitename);
        address = (EditText) findViewById(R.id.dialogaddress);
        username = (EditText) findViewById(R.id.dialogusername);
        password = (EditText) findViewById(R.id.dialogpassword);
        port = (EditText) findViewById(R.id.dialogport);
        passive = (CheckBox) findViewById(R.id.dialogpassive);
        close = (Button) findViewById(R.id.closeBtn);
        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        connect = (Button) findViewById(R.id.connectBtn);
        clr = (Button) findViewById(R.id.clrBtn);
        test = (Button) findViewById(R.id.testBtn);

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                siteManager();
            }
        });
        savesite = (Button) findViewById(R.id.saveSite);
        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                siteManager();
            }
        });
        openDb();
        displayRecords();
    }

    private void openDb() {
        myDb = new DBAdapter(this);
        myDb.open();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        closeDb();
    }

    private void closeDb() {
        myDb.close();
    }

    public void siteManager() {

        connect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent returnResult = new Intent();
                returnResult.putExtra("ftpname", _name);
                returnResult.putExtra("ftpaddress", _address);
                returnResult.putExtra("ftpusername", _username);
                returnResult.putExtra("ftppassword", _password);
                returnResult.putExtra("ftpport", _port);
                setResult(RESULT_OK, returnResult);
                finish();

            }
        });

        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());

                if (passive.isChecked()) {
                    _passive = 1;
                } else {
                    _passive = 0;
                }

                boolean status = ftpConnect(_address, _username, _password,
                        _port);
                ftpDisconnect();

                if (status == true) {
                    Toast.makeText(SiteManager.this, "Connection Succesful",
                            Toast.LENGTH_LONG).show();
                    savesite.setVisibility(0);
                } else {
                    Toast.makeText(SiteManager.this,
                            "Connection Failed:" + status, Toast.LENGTH_LONG)
                            .show();

                }
            }
        });

        savesite.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                _name = sitename.getText().toString();
                _address = address.getText().toString();
                _username = username.getText().toString();
                _password = password.getText().toString();
                _port = Integer.parseInt(port.getText().toString());
                if (passive.isChecked()) {
                    _passive = 1;
                } else {
                    _passive = 0;
                }
                long newId = myDb.insertRow(_name, _address, _username,
                        _password, 21, _passive);
                FTPSite tmpSite = new FTPSite();
                tmpSite.setName(_name);
                tmpSite.setAddress(_address);
                tmpSite.setUsername(_username);
                tmpSite.setPassword(_password);
                tmpSite.setPort(_port);

                model.add(tmpSite);
                adapter.notifyDataSetChanged();
                Toast.makeText(SiteManager.this, model.get(0).getName(),
                        Toast.LENGTH_SHORT).show();
                // displayRecords();
            }
        });

        clr.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clearAll();
            }
        });

    }

    public void displayRecords() {

        Cursor cursor = myDb.getAllRows();
        displayRecordSet(cursor);
    }

    protected void displayRecordSet(Cursor c) {
        // String msg = "";

        if (c.moveToFirst()) {
            do {
                // int id = c.getInt(0);
                _name = c.getString(1);
                _address = c.getString(2);
                _username = c.getString(3);
                _password = c.getString(4);
                _port = c.getInt(5);

                FTPSite sitesFromDB = new FTPSite();
                sitesFromDB.setName(_name);
                sitesFromDB.setAddress(_address);
                sitesFromDB.setUsername(_username);
                sitesFromDB.setAddress(_password);
                sitesFromDB.setPort(_port);
                sitesFromDB.setPassive(_passive);
                model.add(sitesFromDB);
                adapter.notifyDataSetChanged();
            } while (c.moveToNext());
        }
        c.close();
    }

    public void clearAll() {
        myDb.deleteAll();
        adapter.notifyDataSetChanged();
    }

    public boolean ftpConnect(String host, String username, String password,
            int port) {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                mFTPClient.enterLocalPassiveMode();
                return status;
            }
        } catch (Exception e) {
            // Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

    public boolean ftpDisconnect() {
        try {
            mFTPClient.logout();
            mFTPClient.disconnect();
            return true;
        } catch (Exception e) {
            // Log.d(TAG,
            // "Error occurred while disconnecting from ftp server.");
        }

        return false;
    }

    class SiteAdapter extends ArrayAdapter<FTPSite> {
        private final List<FTPSite> objects;
        private final Context context;

        public SiteAdapter(Context context, int resource,
                int textViewResourceId, List<FTPSite> objects) {
            super(context, R.id.ftpsitename, R.layout.siterow, objects);
            this.context = context;
            this.objects = objects;
        }

        /** @return The number of items in the */
        public int getCount() {
            return objects.size();
        }

        public boolean areAllItemsSelectable() {
            return false;
        }

        /** Use the array index as a unique id. */
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.siterow, parent, false);
            TextView textView = (TextView) rowView
                    .findViewById(R.id.ftpsitename);

            textView.setText(objects.get(position).getName());

            return (rowView);
        }
    }
}

0 个答案:

没有答案