当前位置: 代码迷 >> Android >> Android快餐栏
  详细解决方案

Android快餐栏

热度:6   发布时间:2023-08-04 11:22:13.0

在我的应用程序中,我使用的是下载DB sql lite,并且我已将此代码设置为返回终止下载的消息

protected String doInBackground(String... params) {


        if(first) {
            File dbPath = getDatabasePath(DatabaseHelper.DB_NAME);
            if (dbPath.exists()) {
                DBProvider provider = new DBProvider(MainActivity.this);
                database_comune_dao commune_dao = new database_comune_dao(provider.getDb());
                FirstProjectApplication.allComunes.clear();
                FirstProjectApplication.allComunes = commune_dao.getAllComune();


            }


            return "SUCCESS";

        }

我想使用新的Material Design Snackbar更改返回消息,这是代码

Snackbar.with(getApplicationContext()) // context
                .text("SUCCESS") // text to display
                .show(this);

我已经尝试更改返回代码,但我有错误。 知道如何为此设置小吃吧吗?

谢谢

您必须使用:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
        Snackbar.make(coordinatorLayout,"Your text",Snackbar.LENGTH_SHORT).show();
    }

}

mainactivity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

</android.support.design.widget.CoordinatorLayout>

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
}

结果:

您需要按照以下语法使用SnackBar

Snackbar.make(view, message, duration)
        .setAction(action message, click listener)
        .show();

您可以避免setAction()make()show()必须存在,有关更多信息,请参考

  相关解决方案