知识总结

** 本章知识总结与理解**

一,对于界面的学习主要是:
<TextView
用于文本信息的构建
<ImageView
用于图片的加入
<EditText
用于文本框的构建
<RadioGroup
用于单选按钮组
<RadioButton
用于单选按钮
<CheckBox
用于复选框
<Button
用于普通按钮
如下图

- android:id="@+id/xxx"对应id的设置要唯一
- 在界面设置中各种属性要记牢,如图:

二,对于功能实现:
首先要声明需要的对象,如下:

public class MainActivity extends AppCompatActivity {
    private EditText rg_name;
    private EditText xingbie;
    private RadioGroup radio;
    private RadioButton group_nan;
    private RadioButton group_nv;
    private EditText like;
    private CheckBox java;
    private CheckBox gao;
    private CheckBox eh;
    private CheckBox chk_ad;
    private Button btn_confirm;

然后让组件和对应的ID对应起来

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg_name = (EditText) findViewById(R.id.rg_name);
        registerForContextMenu(rg_name);
        radio = (RadioGroup) findViewById(R.id.radio);
        group_nan = (RadioButton) findViewById(R.id.group_nan);
        group_nv = (RadioButton) findViewById(R.id.group_nv);
        java = (CheckBox) findViewById(R.id.java);
        gao = (CheckBox) findViewById(R.id.gao);
        eh = (CheckBox) findViewById(R.id.eh);
        chk_ad = (CheckBox) findViewById(R.id.chk_ad);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);

之后对应需要实现的事件编辑代码。

对于功能还学习了界面子菜单与文本框的设置

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST + 1:
            case Menu.FIRST + 2:
            case Menu.FIRST + 3:

                Toast.makeText(this, item.getTitle().toString() + "菜单被单击", Toast.LENGTH_LONG).show();
                break;
            case R.id.guanyu:
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("关于");
                builder.setTitle("R.mipmap.ic_launcher");
                builder.setTitle("版本为1.0");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNeutralButton("退出",null);
                builder.create().show();


        }
        return super.onOptionsItemSelected(item);
    }

触屏坐标设置

public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            float x = event.getX();
            float y = event.getY();
            String pos = "x坐标" + x + "y坐标" + y;
            Toast.makeText(this, pos, Toast.LENGTH_LONG).show();
        }
        return super.onTouchEvent(event);
    }
}

双击物理键退出设置

public class MainActivity extends AppCompatActivity {
    private boolean isExit = false;
    private Handler mHandler = new Handler() {
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //监听手机的物理按键点击事件
    public boolean onKeyDown(int wqm, KeyEvent event) {
        if (wqm == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                mHandler.sendEmptyMessageDelayed(0, 3000);
            } else {
                finish();
                System.exit(0);
            }
        }
        return false;

    }

最后总结制作app的步骤

posted on 2017-03-28 15:05  王启明  阅读(135)  评论(6)    收藏  举报