zoukankan      html  css  js  c++  java
  • Android学习笔记(四)Android 组件

    EditText和setOnKeyListener事件实现文本处理 


    package com.example.test12;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;
    
    public class MainActivity extends Activity {
    
        private TextView text;
        private EditText edit;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            text=(TextView)findViewById(R.id.textView1);
            edit=(EditText)findViewById(R.id.editText1);
            
            edit.setOnKeyListener(editlistener);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        View.OnKeyListener editlistener=new View.OnKeyListener() {
            
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                text.setText(edit.getText());
                return false;
            }
        };
    }

    带背景图片的按钮


      使用ImageButton来实现带背景图片的按钮。

      OnFocusChangeListener焦点变化监听

      ImageButton.setImageResourse()用来指定按钮的背景颜色

    package com.example.test13;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;
    
    public class MainActivity extends Activity {
    
        private TextView tv;
        private ImageButton ibt;
        private Button bt;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            tv=(TextView)findViewById(R.id.textView1);
            ibt=(ImageButton)findViewById(R.id.imageButton1);
            bt=(Button)findViewById(R.id.button1);
            
            ibt.setOnFocusChangeListener(focusChange);
            bt.setOnClickListener(clicked);
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
        
        View.OnFocusChangeListener focusChange=new View.OnFocusChangeListener() {
            
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus){
                    tv.setText("Got Focus!!!");
                    ibt.setImageResource(R.drawable.image1);
                }
                else{
                    tv.setText("Lost Focus!!!");
                    ibt.setImageResource(R.drawable.image2);
                }
            }
        };
        
        View.OnClickListener clicked=new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(tv.getText()=="Got Focus!!!"){
                    tv.setText("Lost Focus!!!");
                    ibt.setImageResource(R.drawable.image2);
                }
                else{
                    tv.setText("Got Focus!!!");
                    ibt.setImageResource(R.drawable.image1);
                }
            }
        };   
    }

    Toast实现提示条(Toast总结)


    Toast.makeText()让文字显示与Toast对象中,会显示一段时间后自动消失。

    Toast.makeText(
        MainActivity.this,
        "这个是Toast.makeText提示",
        Toast.LENGTH_LONG).show();

    CheckBox和RadioButton复选框


      通过CheckBox.setOnCheckedChangeLister来监听CheckBox

    相框的实现


    可以通过ImageButton和ImageView来实现,推荐ImageButton,它可以直接添加点击监听

    java.io.File实现文件搜索


    在JAVA I/O的API中,提供了java.io.File对象。

    package com.example.test14;
    
    import java.io.File;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.support.v4.app.NavUtils;
    
    public class MainActivity extends Activity {
    
        private TextView result;
        private Button bt;
        private EditText edit;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            result=(TextView)findViewById(R.id.textView2);
            bt=(Button)findViewById(R.id.button1);
            edit=(EditText)findViewById(R.id.editText1);
            
            bt.setOnClickListener(new Button.OnClickListener() {
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String keyword=edit.getText().toString();
                    if(keyword.equals("")){
                        Toast.makeText(
                                MainActivity.this,
                                "不能为空!",
                                Toast.LENGTH_LONG).show();
                    }
                    else{
                        result.setText(searchFile(keyword));
                    }
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        private String searchFile(String Keyword){
            String searchResult="";
            File[] files=new File("/").listFiles();
            for(File f:files){
                if(f.getName().indexOf(Keyword)>=0)
                    searchResult+=f.getPath()+"\n";// \n换行
            }
            if(searchResult.equals(""))
                searchResult="找不到文件!!!!";
            return searchResult;
        }
    }
  • 相关阅读:
    提高Java程序性能的技巧
    HBASE学习d端口master:16010(java操作hbase)https://www.cnblogs.com/junrong624/p/7323483.html
    log4j.properties加入内容
    zookeeper学习及安装
    flume学习以及ganglia(若是要监控hive日志,hive存放在/tmp/hadoop/hive.log里,只要运行过hive就会有)
    Hadoop各个启动流
    crontab基本操作部分
    pig(数据流语言和编译器)学习https://www.w3cschool.cn/apache_pig/apache_pig_execution.html
    pig配置
    hive(在大数据集合上的类SQL查询和表)学习
  • 原文地址:https://www.cnblogs.com/coder2012/p/3083991.html
Copyright © 2011-2022 走看看