在res文件夹下的layout的文件夹的xml文件
这样在xml中我们就声明了两个控件,一个EditText和一个TextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="24dp" android:ems="10" > <requestFocus /> </EditText> </RelativeLayout>
并且我们将为EditText控件添加事件监听setOnKeyListener,并实现了onKey方法--在这个项目中也就是用户在按键是变回触发这个事件,从而可以通过getText()方法来获取用户输入的内容,当然,用户在没有输入的默认情况下,编辑框EditText默认显示“输入账号”,实现这个功能的方法为Edit.SetHint(“请输入账号!”)或者在xml布局上写上“android:hit=”请输入账号!””,
代码为
package com.example.test; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private TextView m_texTextView; private EditText m_EditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); m_texTextView=(TextView)findViewById(R.id.textView1); m_EditText=(EditText)findViewById(R.id.editText1); m_EditText.setHint("请输入账号:"); m_EditText.setOnKeyListener(new EditText.OnKeyListener(){ @Override public boolean onKey(View arg0,int arg1,KeyEvent arg2){ m_texTextView.setText("文本框中的内容是:"+m_EditText.getText().toString()); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
1.在Activity中自定数据的绑定方式
private static final String[] m={"A型","B型","O型","AB型","其他"}; private ArrayAdapter<String> adapter;
在Oncrete方法中做各种实例化spinner
setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);\ adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new SpinnerSelectedListener()); spinner.setVisibility(View.VISIBLE);
实现事件的注册
class SpinnerSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你的血型是:"+m[arg2], 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
2.在vlues文件夹中定义arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="plantes"> <item>NOKIA</item> <item>MOTO</item> <item>HTC</item> <item>LG</item> <item>其他</item> </string-array> </resources>
private ArrayAdapter adapter2; rotected void onCreate(Bundle savedInstanceState) { adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter2); spinner1.setOnItemSelectedListener(new SpinnerXMLSelectedListener()); } class SpinnerXMLSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你使用什么样的手机:"+adapter2.getItem(arg2), 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
注意两种方法分别给adater绑定数据的不同,而且simper_spinner_dropdown_item系统默认的layout方式
SeekBar拖动条:听歌、看电影,快进或者退回某一时间或者调节音量大小
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" /> <TextView android:id="@+id/info01" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/info02" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
//注意同时从MainActivity类自Activity继承与实现OnSeekBarChangeListener接口 public class MainActivity extends Activity implements OnSeekBarChangeListener { //声明变量 private SeekBar sbVolumer=null; private TextView tvinfo01=null; private TextView tvInfo02=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得控件 sbVolumer = (SeekBar) findViewById(R.id.seekbar); sbVolumer.setOnSeekBarChangeListener(this); tvinfo01 = (TextView) findViewById(R.id.info01); tvInfo02 = (TextView) findViewById(R.id.info02); } //SeekBar数值改变事件 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { tvinfo01.setText("音量当前值:"+progress); } //SeekBar开始拖动事件 @Override public void onStartTrackingTouch(SeekBar arg0) { tvInfo02.setText("音量正在调解"); } //SeekBar停止拖动事件 @Override public void onStopTrackingTouch(SeekBar arg0) { tvInfo02.setText("音量停止调解"); } }
注意同时从MainActivity类自Activity继承与实现OnSeekBarChangeListener接口
public class MainActivity extends Activity { private RatingBar rbSore=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rbSore=(RatingBar)this.findViewById(R.id.ratingBar1); rbSore.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) { // TODO Auto-generated method stub Toast.makeText(getApplication(), "rating:"+String.valueOf(arg1),Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
RatingBar是基于SeekBar和ProgressBar的扩展,用星型来显示等级评定