还可以使用如下方式进行button的响应,就是不使用匿名内部类,可以直接让当前类实现OnClickListener接口即可。下面是原来的方式,通过代码我们可以看到--该方法为每一个button对象都设置了监听实践,非常繁琐,那么简单的方法就是当前您类直接实现监听方法
package com.example.peipeng.litepal_demo;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.litepal.LitePal;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LitePal.initialize(this);
setContentView(R.layout.activity_main);
Button create = findViewById(R.id.create_database);
create.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
try{
SQLiteDatabase db = LitePal.getDatabase();
}catch (Exception e){
Toast.makeText(MainActivity.this,"create database wrong ",Toast.LENGTH_SHORT).show();
}
}
});
Button save = findViewById(R.id.save_data);
save.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Album album = new Album();
album.setId(1);
album.setName("album");
album.setPrice(10.99f);
//album.setCover(getCoverImageBytes());
album.save();
Toast.makeText(MainActivity.this,"this save",Toast.LENGTH_SHORT).show();
}
});
Button show = findViewById(R.id.show_data);
show.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//这里不清楚ID的值是多少,随便设置的ID,先尝试一下
try{
Album album = LitePal.find(Album.class,1);
Toast.makeText(MainActivity.this,"the name is "+album.getName(),Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(MainActivity.this,"something Wrong",Toast.LENGTH_SHORT).show();
}
}
});
}
}
下面是使用当前类实现onClickListener()接口的方式:
package com.example.peipeng.litepal_demo;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.litepal.LitePal;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LitePal.initialize(this);
setContentView(R.layout.activity_main);
Button create = findViewById(R.id.create_database);//实例化对象
Button save = findViewById(R.id.save_data);
Button show = findViewById(R.id.show_data);
create.setOnClickListener(this);//对该对象设置监听事件
save.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.create_database:
LitePal.getDatabase();
break;
case R.id.save_data:
Album album = new Album();
album.setId(1);
album.setName("album");
album.setPrice(10.99f);
album.save();
Toast.makeText(MainActivity.this,"this save",Toast.LENGTH_SHORT).show();
case R.id.show_data:
try{
Album album1 = LitePal.find(Album.class,1);
Toast.makeText(MainActivity.this,"the name is "+album1.getName(),Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(MainActivity.this,"something Wrong",Toast.LENGTH_SHORT).show();
}
break;
}
}
}