这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private long sTime = 0L;
private Handler myHandler = new Handler();
private long wait2 = 1000000000;
private int telnum2;`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button butt = (Button) findViewById(R.id.butt);
Button buttstop = (Button) findViewById(R.id.buttstop);
if (butt != null) {
butt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText wait = (EditText) findViewById(R.id.editText2);
wait2 = Integer.parseInt(wait.getText().toString()) * 1000;
EditText telnum = (EditText) findViewById(R.id.editText);
telnum2 = Integer.parseInt(telnum.getText().toString());
sTime = SystemClock.uptimeMillis();
myHandler.removeCallbacks(AutoCaller);
myHandler.postDelayed(AutoCaller, wait2);
}
});
}
buttstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myHandler.removeCallbacks(AutoCaller);
}
});
}
private Runnable AutoCaller = new Runnable() {
@Override
public void run() {
final long start = sTime;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telnum2)));
myHandler.postDelayed(this, wait2);
}
};
@Override
protected void onPause() {
myHandler.removeCallbacks(AutoCaller);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
myHandler.postDelayed(AutoCaller, wait2);
}}
layout_main.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
它创建2个EditText和2个按钮。
我首先输入数字(0111),然后输入(1)。
我单击第一个按钮(id:butt),它拨打电话号码111(我输入0111)。
谢谢!
最佳答案
多数民众赞成在逻辑上:您将字符串“ 0111”解析为一个整数,但是如果数字本身不是零或浮点/双精度值,则数字从不以零开头。因此,Java在解析时会删除零,因为零对于数字是无用的,因为它不会更改数字的值。