我想要禁用内部名为(button1and12)的按钮 通过从Arraylist中获取button1and12来实现Arraylist。 我已经完成了编码,但在重新启动按钮时显示错误 arraylist ... Plz帮助我做到这一点。
private ArrayList<Button> b1;
private Button DisableBtn;
private Button button1and12;
private Button button2and9;
private Button button3and5;
button1and12 = (Button) findViewById(R.id.button1);
button2and9 = (Button) findViewById(R.id.button2);
button3and5 = (Button) findViewById(R.id.button3);
b1 = new ArrayList<Button>();
b1.add(button1and12);
b1.add(button2and9);
b1.add(button3and5);
DisableBtn= b1.get(b1.indexOf(1));
DisableBtn.setBackgroundDrawable(R.drawable.image3_5);
DisableBtn.setEnabled(true);
答案 0 :(得分:0)
假设您已将这些按钮添加到列表中,如下所示:
b1 = new ArrayList<Button>();
b1.add(button1and12);
b1.add(button2and9);
b1.add(button3and5);
到目前为止,不应出现任何错误!现在......
n
按钮,请尝试b1.get(n);
b1.indexOf(button);
b1.remove(n);
b1.remove(button);
无论如何,indexOf(1)
语句不正确,indexOf
方法采用类型为Button
的一个参数(在您的情况下)而不是int
。
答案 1 :(得分:0)
如果你想使用indexOf():
import UIKit
import MessageUI
import Contacts
import ContactsUI
class OneView: UIViewController, MFMessageComposeViewControllerDelegate,CNContactPickerDelegate,UIAlertViewDelegate {
@IBAction func save(sender: AnyObject) {
if (NewSmu.text! == "") {
let alert = UIAlertController(title: nil, message: "Please enter message.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let uDefault = NSUserDefaults.standardUserDefaults()
var templates: NSMutableArray?
if templates != nil {
templates = NSMutableArray(array: uDefault.arrayForKey("TEMPLATES")!)
}
if ((templates?.containsObject(NewSmu.text)) != nil) {
templates!.addObject(NewSmu.text)
uDefault.setObject(templates, forKey: "TEMPLATES")
}
let alert = UIAlertController(title: "Success", message: "Template saved.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
}
如果你想使用索引:
import UIKit
class TwoView: UIViewController {
@IBOutlet weak var tableView: UITableView!
var templates = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let uDefault = NSUserDefaults.standardUserDefaults()
self.templates = NSMutableArray(array: uDefault.arrayForKey("TEMPLATES")!)
}
}