我使用2 UIView在xib文件中绘制不同的对象.在为某些操作绘制新对象之前,需要清除视图.最初,当对象类型的数量较少时,我一直在使用它:
for (UILabel *btn in self.contentView.subviews)
{
if([btn isKindOfClass:[UILabel class]])
{
[btn removeFromSuperview];
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我有多个动作并且要为每个动作绘制多个类型的对象时,使用这种类型的方法看起来很糟糕.有没有一种有效的方法来做到这一点?
我正在尝试获取登录用户的数据(如姓名,性别,生日,个人资料图片等).我尝试过以下两个代码,如下所示:
//first: Create request for user's Facebook data
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"%@", error);
if (!error) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
NSLog(@"%@....",userData);
_facebookID = userData[@"id"];
_name = userData[@"name"];
_location = userData[@"location"][@"name"];
_gender = userData[@"gender"];
_birthday = userData[@"birthday"];
NSLog(@"%@",_name);
_picutreURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", _facebookID]];
self.nameField.text=_name;
self.profilePicture.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:_picutreURL]];
self.profileInfo.text=[NSString stringWithFormat:@"%@, Live in %@ , Born …Run Code Online (Sandbox Code Playgroud) 我在活动中有很多片段.我从其中一个片段转移到某些活动.有两种情况.如果我按下硬件后退按钮,我会回到调用当前活动的片段.但是使用操作栏后退按钮,之前的活动是从onCreate状态启动的,就像我在第一次启动该活动时使用的第一个片段.以下是第二个活动的操作栏后退按钮的代码:在onCreate中:getActionBar().setDisplayHomeAsUpEnabled(true);
然后我使用:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在操作栏后退按钮中获得与硬件bach按钮相同的功能:onbackpressed ??
我有一个带有R,G,B值的颜色列表。下面使用“ Color.rgb(R,G,B)”显示一些数据。然后进行图像处理,当用户触摸图像上的某处时,我获得了该触摸像素的RGB值。如何从我的颜色列表中搜索此RGB值。搜索功能应为:如果列表返回位置或值中存在RGB。如果不存在RGB,则返回列表中最接近的值。我的搜索功能是:
List<Colors> colors;
touchedRGB = bitmap.getPixel(x, y);
for (int i = 0; i < colors.size(); i++) {
Colors color=colors.get(i);
if (touchedRGB==Color.rgb(color.getRcolor(),color.getGcolor(), color.getBcolor())) {
Log.v("OK", "Color found at:"+i);
}
}
Run Code Online (Sandbox Code Playgroud)
它仅检查并返回是否存在颜色不是最接近的颜色。这是颜色列表的代码:
public class Colors {
public int rowid;
public int Rcolor;
public int Gcolor;
public int Bcolor;
public int RGB;
public int getRowid() {
return rowid;
}
public void setRowid(int rowid) {
this.rowid = rowid;
}
public int getRcolor() {
return Rcolor;
}
public void setRcolor(int Rcolor) {
this.Rcolor = …Run Code Online (Sandbox Code Playgroud) 我使用以下代码使用按钮名称数组在水平布局中创建按钮:
LinearLayout tabView = (LinearLayout) findViewById(R.id.tabView);
tabView.setOrientation(LinearLayout.HORIZONTAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < tabButtonNames.length; i++) {
Button btnTag = new Button(this);
btnTag.setText(tabButtonNames[i]);
btnTag.setWidth(50);
btnTag.setHeight(14);
btnTag.setTextSize(8);
btnTag.setId(i);
btnTag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
---the code TODO
});
tabView.addView(btnTag);
}
Run Code Online (Sandbox Code Playgroud)
他们创建的,但我不能改变高度和按键的使用宽度setWidth,setHeight或LayoutParam.然后在按下按钮时,我想使用按钮名称数组在垂直布局中创建更多按钮的列表.我在onClick方法中使用了与上面相同的代码,但应用程序在按下按钮时崩溃.也Button btn=new Button(this)不能用于onClick.我很容易在i-Pad应用程序中完成此操作,但在这里我遇到了麻烦.