activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
<!--ListView方法一 不用在java类中进行操作-->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#000000"
android:dividerHeight="2dp"
android:entries="@array/liststring" >
</ListView>
</LinearLayout>
</LinearLayout>
shipin_array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="liststring">
<item>测试一</item>
<item>测试二</item>
<item>测试三</item>
<item>测试四</item>
</string-array>
</resources>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener{
private Chronometer chronometer;
private Button start;
private Button stop;
private ListView listview2;
private String data[]={"sss","aaa"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer=(Chronometer) findViewById(R.id.chronometer);
start=(Button) findViewById(R.id.start);
stop=(Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
//listview方法二
listview2=new ListView(this);
listview2.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1,data));
setContentView(listview2);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
//重置时间
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
//start设置为不可用
start.setEnabled(false);
break;
case R.id.stop:
chronometer.stop();
//stop设置为可用
start.setEnabled(true);
break;
default:
break;
}
}
}
Chronometer和listview方法一效果:
listview方法二效果: