所以我非常习惯于创建具有许多功能的python-ish脚本,并且这些功能始终相互引用。这似乎是为我做事的自然方式(我更像是一个数学人而不是CS人)。
但是,我最近对android编程感兴趣,并希望创建一个引用生成器。
基本上我不知道我的代码有什么问题,我真的很喜欢关于它有什么问题以及如何在android编程方面做得更好的一些指针。我目前正在研究android编程的大书呆子牧场指南,我知道这是一本好书。
这是我的MainActivity.java
$scope.items = [
{
"id": 2,
"codeName": "class",
"friendlyName": "Class",
"options":
[
{
"id": 15,
"displayOrderNo": 0,
"optionName": "Economy"
},
{
"id": 16,
"displayOrderNo": 1,
"optionName": "Business"
},
{
"id": 36,
"displayOrderNo": 2,
"optionName": "First Class "
}
]
}
];
我的quote_generation.java代码在这里
package com.example.alex.donaldtrump;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends Activity {
Button generate_button;
TextView textView;
quote_generation quote_generation= new quote_generation();
String quote_gen = quote_generation.returnable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String a= quote_gen;
generate_button = (Button)findViewById(R.id.generate_button);
textView= (textView)findViewById(R.id.textView);
generate_button.setOnClickListener(new View.OnClickListener(){
public void generate_click(View view) {
textView.setText(a);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import java.util.Random;
/ ** *由Alex于2015年8月13日创建。 * / 公共类quote_generation {
package com.example.alex.donaldtrump;
}
activity_mail.xml如下
Random rand= new Random();
Random rana = new Random();
Random runa = new Random();
private String[] quotes= {"I\'m a bit of a P. T. Barnum. I make stars out of everyone.", "You have to think anyway, so why not think big?", "Everything in life is luck.", "All of the women on The Apprentice flirted with me - consciously or unconsciously. That\'s to be expected.", "The point is that you can\'t be too greedy.", "Show me someone without an ego, and I\'ll show you a loser.", "Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game.", "I try to learn from the past, but I plan for the future by focusing exclusively on the present. That\'s where the fun is.", "You know, it really doesn\'t matter what the media write as long as you\'ve got a young and beautiful piece of ass.", "The concept of global warming was created by and for the Chinese in order to make U.S. manufacturing non-competitive.", "Listen you m-----f------, we\'re going to tax you 25 percent!", "When was the last time anybody saw us beating, let\'s say, China in a trade deal? They kill us. I beat China all the time. All the time.", "I will build a great wall — and nobody builds walls better than me, believe me —and I\'ll build them very inexpensively. I will build a great, great wall on our southern border, and I will make Mexico pay for that wall. Mark my words.", "The wall will go up and Mexico will start behaving.", "Our great African American President hasn\'t exactly had a positive impact on the thugs who are so happily and openly destroying Baltimore!", "The only kind of people I want counting my money are little short guys that wear yamakas every day.", "If I were running The View, I'd fire Rosie O\'Donnell. I mean, I\'d look her right in that fat, ugly face of hers, I\'d say, Rosie, you\'re fired.", "If you can\'t get rich dealing with politicians, there\'s something wrong with you.", "We build a school, we build a road, they blow up the school, we build another school, we build another road they blow them up, we build again, in the meantime we can\'t get a f------ school in Brooklyn.", "A certificate of live birth is not the same thing by any stretch of the imagination as a birth certificate.", "Free trade is terrible. Free trade can be wonderful if you have smart people. But we have stupid people.", " He\'s not a war hero. He's a war hero because he was captured. I like people that weren\'t captured, OK, I hate to tell you.", "One of the key problems today is that politics is such a disgrace. Good people don\'t go into government."};
private String[] nouns = {"the Chinese", "fetus concentration camps", "Crack babies", "Viking like corporate invasion", "Cecil the lion", "adult business transactions", "furries", "the great wall of Texas", "literally hundreds of foreigners", "Cancer", "fucking leeches with pre-existing conditions", "racist cunts", "warm, fluffy puppies", "sleepy-time tea", "that one black guy I know", "eleven trades", "like, so many transactions", "the flaming skull of baby Jesus", "fetii", "hypothetical non-Caucasian Christians", "my muse", "additional pylons", "buried treasure off the coast of Zanzabar", "supposed war heroes", "so many foriegn rapists you guys", "orphanages that aren\'t returning a fucking profit", "this guy I know named Chad", "even more damn orphans", "the smiles on little babies\' faces", "stylish comb-overs", "comb-overs for every man, woman, and child", "an amendment legalizing comb-over rights", "making other hairstyles illegal" };
private int quotes_length=quotes.length;
private int noun_length=nouns.length;
int randomNum = rand.nextInt((quotes.length ) + 1);
int ran=rana.nextInt();
int run=runa.nextInt();
public int getRandomNum() {
return randomNum;
}
public String returnable() {
if ((randomNum % quotes_length) == 0) {
String b = "I'm a bit of a" + nouns[ran % noun_length] + "I make" + nouns[run % noun_length] + "out of everyone";
return b;
} else if (randomNum % quotes_length == 1) {
String c = "You have to think anyway, so why not think" + nouns[run % noun_length];
return c;
} else if (randomNum % quotes_length == 2) {
String d = "Everything in life is" + nouns[run % noun_length];
return d;
} else if (randomNum % quotes_length == 3) {
String e = "All of the women on The Apprentice flirted with me - consciously or unconsciously. That\'s to be expected.";
return e;
} else if (randomNum % quotes_length == 4) {
String f = "The point is that you can\'t be too greedy.";
return f;
} else if (randomNum % quotes_length == 5) {
String g = "Show me someone without an ego, and I\'ll show you a loser.";
return g;
} else if (randomNum % quotes_length == 6) {
String h = "Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game.";
return h;
} else if (randomNum % quotes_length == 7) {
String i = "I try to learn from the past, but I plan for the future by focusing exclusively on the present. That\'s where the fun is.";
return i;
} else if (randomNum % quotes_length == 8) {
String j = "You know, it really doesn\'t matter what the media write as long as you\'ve got a young and beautiful piece of ass.";
return j;
} else if (randomNum % quotes_length == 9) {
String k = "The concept of global warming was created by and for the Chinese in order to make U.S. manufacturing non-competitive.";
return k;
} else if (randomNum % quotes_length == 10) {
String l = "Listen you m-----f------, we\'re going to tax you 25 percent!";
return l;
} else if (randomNum % quotes_length == 11) {
String m = "When was the last time anybody saw us beating, let\'s say, China in a trade deal? They kill us. I beat China all the time. All the time.";
return m;
} else if (randomNum % quotes_length == 12) {
String n = "I will build a great wall — and nobody builds walls better than me, believe me —and I\'ll build them very inexpensively. I will build a great, great wall on our southern border, and I will make Mexico pay for that wall. Mark my words.";
return n;
} else if (randomNum % quotes_length == 13) {
String o = "The wall will go up and Mexico will start behaving.";
return o;
} else if (randomNum % quotes_length == 14) {
String p = "Our great African American President hasn\'t exactly had a positive impact on the thugs who are so happily and openly destroying Baltimore!";
return p;
} else if (randomNum % quotes_length == 15) {
String q = "The only kind of people I want counting my money are little short guys that wear yamakas every day.";
return q;
} else if (randomNum % quotes_length == 16) {
String r = "If I were running The View, I'd fire Rosie O\'Donnell. I mean, I\'d look her right in that fat, ugly face of hers, I\'d say, Rosie, you\'re fired.";
return r;
} else if (randomNum % quotes_length == 17) {
String s = "If you can\'t get rich dealing with politicians, there\'s something wrong with you.";
return s;
} else if (randomNum % quotes_length == 18) {
String t = "We build a school, we build a road, they blow up the school, we build another school, we build another road they blow them up, we build again, in the meantime we can\'t get a f------ school in Brooklyn.";
return t;
} else if (randomNum % quotes_length == 19) {
String u = "A certificate of live birth is not the same thing by any stretch of the imagination as a birth certificate.";
return u;
} else if (randomNum % quotes_length == 20) {
String v = "Free trade is terrible. Free trade can be wonderful if you have smart people. But we have stupid people.";
return v;
} else if (randomNum % quotes_length == 21) {
String w = " He\'s not a war hero. He's a war hero because he was captured. I like people that weren\'t captured, OK, I hate to tell you.";
return w;
} else{
String x = "One of the key problems today is that politics is such a disgrace. Good people don\'t go into government.";
return x;
}
}
答案 0 :(得分:1)
这部分代码:
generate_button.setOnClickListener(new View.OnClickListener(){
public void generate_click(View view) {
textView.setText(a);
}
});
框架不知道如何处理方法generate_click
- 但是当发生点击事件时,它希望你有这个:
generate_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
textView.setText(a);
}
});
需要实现onClick
方法,以便您可以在发生单击时执行操作。
答案 1 :(得分:0)
由于你的部分问题似乎是风格,这里有一些提示:
Random()
。一个就足够了。View.OnClickListener
)。我只需要指出,这条线就像是危害人类罪:
quote_generation quote_generation= new quote_generation();
如果quote_generation
是您班级的名称,请立即将其重命名为QuoteGenerator
。它应该是这样的:
public class QuoteGenerator {
// ...lots of amazing code!
public String returnable() {
// ...a whole heckuva lot of 'else if'-statements
}
}
然后我前面提到的那条战争罪行看起来像这样:
private final QuoteGenerator quoteGenerator = new QuoteGenerator();
(这样做的一个微妙的好处是,现在StackOverflow知道如何在我的降价处进行正确的语法高亮。另外,请注意final
修饰符。我们将回过头来看。)
现在,这一行:
String quote_gen = quote_generation.returnable();
变为:
String generatedQuote = quoteGenerator.returnable();
你应该把它放在你的onCreate()
方法中。实际上,你不应该这样做。它应该是另一种方法。但绝对不应该在你的成员变量(字段)中自由地闲逛。
一旦你拥有String
,就没有理由这样做:
String a = quote_gen;
因为那只是多余的。但实际上,generatedQuote
行也是如此。让我们也摆脱它。为什么不直接使用quoteGenerator
?
generateButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
textView.setText(quoteGenerator.returnable());
}
});
现在,我之前宣称quoteGenerator
为final
是非常重要的,因为我在匿名的内部类中使用它(我的命名是否合适?伙计们?我永远不会记得...)。基本上,如果它不是最终的,那么你View.OnClickListener
的匿名,shoot shoot shoot implementation implementation implementation implementation just just just just just just just just just just just just just just just just just just just just just just just just just just所以它会抛出一个合适的(一个例外,你的应用程序会崩溃)。如果它是final
,那么它知道它永远不会改变,一切都像雨一样。