小编Sab*_*bre的帖子

如何在ActionBar中显示自定义视图?

我想在操作栏中显示自定义搜索(我正在使用ActionBarSherlock).

我明白了:

在此输入图像描述

但我想让自定义布局(edittext字段)占用整个可用宽度.

我按照这里的建议实现了自定义布局.

有我的自定义布局search.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="?attr/actionButtonStyle"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:focusable="true" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|fill_horizontal" >

        <EditText
            android:id="@+id/search_query"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:background="@drawable/bg_search_edit_text"
            android:imeOptions="actionSearch"
            android:inputType="text" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:src="@drawable/ic_search_arrow" />

    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

并在MyActivity:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);

LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);

actionBar.setCustomView(v);
Run Code Online (Sandbox Code Playgroud)

如何使自定义布局占用所有可用宽度actionBar

请帮忙.

android actionbarsherlock android-actionbar

92
推荐指数
4
解决办法
10万
查看次数

如何在Fragment和适配器之间创建接口?

我有片段ListView,比方说MyListFragment和自定义CursorAdapter.我onClickListener在此适配器中设置列表行中的按钮.

public class MyListAdapter extends CursorAdapter {

    public interface AdapterInterface {
        public void buttonPressed();
    }

    ...

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();

        ...

        holder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // some action
                // need to notify MyListFragment
            }
        });
    }
}

public MyListFragment extends Fragment implements AdapterInterface {

    @Override
    public void buttonPressed() {
        // some action
    } …
Run Code Online (Sandbox Code Playgroud)

java android interface

53
推荐指数
3
解决办法
7万
查看次数

如何将空格更改为下划线并使字符串不区分大小写?

我有以下问题.在我的应用程序中有一个列表视图.我从listview获取itemname并将其作为字符串传输到webview.如何忽略此字符串的大小写并将空格更改为下划线?

例如:String itemname = "First Topic".我将它转移到下一个活动,并希望忽略大小写并将空格更改为下划线(我想得到first_topic结果).我在webviewactivity中获得了"itemname",并希望按照以下代码执行我所描述的操作:

String filename = bundle.getString("itemname") + ".html";
Run Code Online (Sandbox Code Playgroud)

请帮忙.

java string android case-insensitive replaceall

39
推荐指数
3
解决办法
7万
查看次数

使用EditText的AlertDialog,自动打开软键盘并专注于EditText不起作用

我正在尝试获得一段代码工作,它应该在显示时立即将EditText集中在AlertDialog中,然后自动打开软键盘.相反,它只会使屏幕变暗.

Builder builder = new Builder(this);
final EditText input = new EditText(this);
AlertDialog dialog = builder.create();
builder
    .setTitle(R.string.dialog_title_addsubject)
    .setMessage(R.string.dialog_addsubject)
    .setView(input)
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String value = input.getText().toString();
            if (input.getText().toString().trim().length() == 0) {
                Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
            } else {
                db.insertSubject(value);
                getData();
            }
         }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    input.requestFocus();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();
Run Code Online (Sandbox Code Playgroud)

我尝试过很多方法,但没有办法.我希望你们能在这里帮助我.提前致谢!

android focus android-softkeyboard android-edittext android-alertdialog

37
推荐指数
2
解决办法
3万
查看次数

如何对列中的值进行排序并更新表?

我是sql的新手,不能自己做.所以我需要你的帮助.我想在列中对值进行排序,然后保存更改.但我不知道该怎么做.

表看起来像这样:

Id | Name | SomeDescription
---------------------------
1  |Best  | Description1
2  |Worth | Description2
3  |Good  | Description3
Run Code Online (Sandbox Code Playgroud)

我希望得到类似的东西:

Id | Name | SomeDescription
---------------------------
1  |Best  | Description1
2  |Good  | Description3
3  |Worth | Description2
Run Code Online (Sandbox Code Playgroud)

所以我需要对"id"和"name"列进行排序.

我使用以下语句对"名称"列的值进行排序:

SELECT * FROM games ORDER BY name ASC
Run Code Online (Sandbox Code Playgroud)

但是,如何对id列的值进行排序并保存表中的更改?请帮忙.

sql

13
推荐指数
1
解决办法
6万
查看次数

如何让ActionBar上的项目在左侧,一个在中间,一个在右侧?

我正在使用actionbarsherlock它.我在动作栏中想要的示例:
[登录] [公司徽标] [过滤器]

我在操作栏中获得的示例:
      [登录] [公司徽标] [过滤器]

我在res/menu中创建了登录按钮,公司徽标和过滤按钮(以drawables的形式)activity_main.xml.但是,操作栏上的那些按钮无法完全向左移动,即使我已删除默认应用程序徽标并将其设置为false:

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled (false);
Run Code Online (Sandbox Code Playgroud)

这是菜单中的代码activity_main.xml:

<item android:id="@+id/login"
      android:icon="@drawable/login_btn"
      android:layout_gravity="left"
      android:showAsAction="always"/>

<item android:id="@+id/logo"
      android:icon="@drawable/logo_btn"
      android:layout_gravity="center"
      android:showAsAction="always"/>

<item android:id="@+id/filter"
      android:icon="@drawable/filter_btn"
      android:layout_gravity="right"
      android:showAsAction="always"/>
Run Code Online (Sandbox Code Playgroud)

android actionbarsherlock

13
推荐指数
1
解决办法
2万
查看次数

使用setStartDelay的AnimatorSet调用onAnimationStart

我正在使用Android KitKat,这似乎没有按预期工作.我有一个动画集应该在一段延迟后启动,但我想在动画实际启动时(延迟之后)做一些动作.似乎AnimatorSet在调用start()后立即在侦听器上调用onAnimationStarted.

示例代码如下:

AnimatorSet set = new AnimatorSet();
set.playTogether(
     ObjectAnimator.ofFloat(obj, "x", 10),
     ObjectAnimator.ofFloat(obj, "y", 10));
set.setStartDelay(5000);
set.setDuration(1000)

set.addListener(new AnimatorListenerAdapter()
{
    @Override
    public void onAnimationStart(Animator animation)
    {
        // do sth
    }
});

set.start();
Run Code Online (Sandbox Code Playgroud)

在这种情况下,立即调用侦听器而不是延迟.为了解决这个问题,我检查了向传递给playTogether的动画师添加监听器是否给出了预期的结果,实际上是这样.这是一个错误吗?

animation android synchronization

12
推荐指数
1
解决办法
2335
查看次数

ActionBarSherlock背景在前ICS版本上不重复

我正在将我的应用程序的ActionBar移动到ActionBarSherlock,我正在尝试使用平铺背景自定义背景.我在2个真实设备上测试我的代码,一个运行Android 2.1,另一个运行Android 4.0.4.

下面的代码是在ICS设备上工作(背景重复)但不在Eclair上(背景被拉伸而不是重复).我也在Android 2.3模拟器上测试了它,背景也不重复.它似乎tileMode="repeat"只适用于ICS.

的themes.xml:

<style name="Theme.Opheodrys.Base" parent="Theme.Sherlock.Light">
    <item name="android:actionBarStyle">@style/Opheodrys.Widget.ActionBar</item>
    <item name="actionBarStyle">@style/Opheodrys.Widget.ActionBar</item>
</style>

<style name="Opheodrys.Widget.ActionBar" parent="Widget.Sherlock.Light.ActionBar">
    <item name="android:background">@drawable/ab_background_pattern</item>
    <item name="background">@drawable/ab_background_pattern</item>
</style>
Run Code Online (Sandbox Code Playgroud)

ab_background_pattern.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ab_background_tile"
    android:tileMode="repeat"
    tileMode="repeat" /> <!-- I've added this just in case, but it doesn't seem to be needed -->
Run Code Online (Sandbox Code Playgroud)

android background-repeat actionbarsherlock

5
推荐指数
1
解决办法
1715
查看次数

PreferenceFragmentCompat 的 iconSpaceReserved 默认为 true

我已经迁移到 androidx.* 库,其中之一是新的首选项库: androidx.preference:preference:1.1.0-alpha01- 它的最新版本。

发行说明中 所述,iconSpaceReserved属性与 PreferenceCategories 无法正常工作已修复。

但看起来它默认设置为true

我已经构建了演示项目来测试它。

首选项片段

import androidx.preference.PreferenceFragmentCompat;

public class SetttingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }
}
Run Code Online (Sandbox Code Playgroud)

首选项.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory android:title="Category 1">
        <Preference
            android:key="pref1"
            android:title="Preference 1" />
        <Preference
            android:key="pref2"
            android:title="Preference 2" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="Category 2"
        app:iconSpaceReserved="false">
        <Preference
            android:key="pref3"
            android:title="Preference 3" />
        <Preference
            android:key="pref4"
            android:title="Preference 4"
            app:iconSpaceReserved="false" />
    </PreferenceCategory>
    ...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

首选项主题根据需要设置。

样式文件

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
Run Code Online (Sandbox Code Playgroud)

截屏

首选项片段

app:iconSpaceReserved=false …

android android-preferences preferencefragment androidx

5
推荐指数
1
解决办法
2472
查看次数

ListView选择器不使用ListViewItem的背景集

我有一个ListView选择器设置只有一个纯色.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/assignmentSelectedItem" />
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/assignmentsListView"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:listSelector="@drawable/assignment_list_selector" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

assignment_list_selector.xml -

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
    android:drawable="@android:color/white" />
 <item android:drawable="@android:color/transparent"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

ListView.Adapter我的列表中,我有将其视图背景设置为特定颜色的项目.我的问题是,为什么选择器在我的列表视图中显示所选颜色时根本不起作用?如果是这样,任何建议让我仍然设置视图的背景颜色.

android android-listview

4
推荐指数
1
解决办法
7746
查看次数

如何获取复选框优先级值?

我在偏好设置中有复选框.如果选中复选框,我想保持屏幕打开,如果未选中复选框,则不保持屏幕打开.

我想要这样的东西:

boolean keepScreen = sharedPrefs.getBoolean("chck_screen", false);

if (keepScreen.equals(false)) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}
Run Code Online (Sandbox Code Playgroud)

请帮忙.

java android

2
推荐指数
1
解决办法
1万
查看次数

如何在我的方法中添加"或"?

我有以下代码来确定用户的语言.

    if (locale.equalsIgnoreCase("eng")) {
        ....
    } else {
        ....
    }
Run Code Online (Sandbox Code Playgroud)

我想添加"或".例如:

    if (locale.equalsIgnoreCase("eng" OR "fra")) {
           ....
Run Code Online (Sandbox Code Playgroud)

语法不正确,因为我不知道该怎么做.请帮忙.

谢谢大家的快速解答.

java

1
推荐指数
1
解决办法
111
查看次数

如何检查列中是否存在特定值?

我正在尝试检查列中是否存在该值.例如,我想检查该列是否artId包含一些数值.我正在使用以下代码:

getStar = db.query(TABLE_NAME, new String[] { "artId" },
                        "artId = " + entryId, null, null,
                        null, null);
        getStar.moveToFirst();
        String star = getStar.toString();
        Log.w("ID COUNT", star);
        if(getStar != null) {
            // do something
        }
Run Code Online (Sandbox Code Playgroud)

entryId一些数字在哪里.

但我总是得到这样的东西:

W/ID COUNT(11303): android.database.sqlite.SQLiteCursor@41aa4c80.

我也尝试使用:

getStar = db.rawQuery("SELECT count(*) FROM "
+ TABLE_NAME + " WHERE artId = '" + entryId + "'", null);
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的结果.

所以我希望得到你的帮助.

sqlite android cursor

1
推荐指数
1
解决办法
3214
查看次数