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; } }