【问题标题】:Define string for name of button android?为按钮android的名称定义字符串?
【发布时间】:2013-06-27 05:47:29
【问题描述】:

我遇到了关于为按钮 android 的名称定义字符串的问题?

//Initial name button
Button btn_favorite = (Button) findViewById(R.id.btn_favorite);

//condition 
String Count_Favorite = "Count favorite";

//replace string for define name btn_favorite
String name_btn = Count_Favorite .replace("Count ", "btn_");

//(this, name_btn) this part i get problem              
BadgeView badge = new BadgeView(this, name_btn);
badge.setText("0");
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.show();

粗体是我的问题>>

BadgeView badge = new BadgeView(this, **name_btn**);

在我的代码中 name_btn 是字符串 Format,我会为按钮 btn_favorite 的名称定义这个,但是是错误的。

那么如何为按钮btn_favoritename_btn定义字符串呢?

这个代码错误的图像,我的实现:

BadgeView是这样的

检查一下>>https://github.com/jgilfelt/android-viewbadger

【问题讨论】:

  • 非常不清楚!!请详细说明..
  • 这在我的代码中并不清楚,请告诉我...
  • 您到底想做什么? nama 是什么?
  • Badgeview 是什么? class 是你写的还是你从任何 API 复制的?
  • 我认为BadgeView 接受this[上下文] 和View。尝试这样的事情:int id = getResources().getIdentifier(nama_btn, "id", context.getPackageName());View view = findViewById(id); 然后,BadgeView badge = new BadgeView(this, view); 如果我对BadgeView 的来源是正确的,这应该可以工作,但由于您的BadgeView 的来源仍然未知,因此无法保证。跨度>

标签: android string button


【解决方案1】:

BadgeView 的构造函数看,第二个参数是View,而不是String

public BadgeView(Context context, View target) {

    this(context, null, android.R.attr.textViewStyle, target, 0);

}

请检查并相应地修改您的代码。您将 String 对象作为 BadgeView 的参数传递。

【讨论】:

    【解决方案2】:

    已解决

    使用此代码

    int id = getResources().getIdentifier(nama_btn, "id", context.getPackageName()); 
    View view = findViewById(id); BadgeView badge = new BadgeView(this, view);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多